Calling a method in parent page & other user control from a user control
时间:2011-06-16 来源:hmloo
1.Calling a method in parent page.
Parent page method:
Public void SetTxt(string txt1, string txt2)
{
}
User control:
private void Button1_Click(object sender, System.EventArgs e)
{
//Get the parent page
Page p = this.Page;
Type pageType = p.GetType ();
//Get method
MethodInfo mi = pageType.GetMethod ("SetTxt");
// Parameter
mi.Invoke (p, new object [] {"parameter 1", "parameter 2"});
}
2. Calling a method in other user control
User control 1:
Public void SetTxt(string txt1, string txt2)
{
}
User control 2:
private void Button1_Click(object sender, System.EventArgs e)
{
//Get the parent page
Page p = this.Page;
//Get the other controls
UserControl uc = p.FindControl ("UserControl1") as UserControl;
Type pageType = uc.GetType ();
//Get method
MethodInfo mi = pageType.GetMethod ("SetTxt");
// Parameter
mi.Invoke (uc, new object [] {"parameter 1", "parameter 2"});
}
3. Set the properties between user controls
// Get the parent page
Page p = this.Page;
/ / Get the other controls
UserControl uc = p.FindControl ("Usercontrol1") as UserControl;
Type pageType = uc.GetType ();
PropertyInfo mi = pageType.GetProperty ("property name");
mi.SetValue (uc, "property value", null);
Note:
1. The method must be Public in parent page or other controls.
2. If you want to get the return value, please get the Invoke return value, it's an object, you can cast to your type.
相关阅读 更多 +