下面演示了用户控件调用父页面SetLaeble方法。
父页面类型用反射的方法获取,这避免了不同页面调用同样时,需要类型转换的问题(不用写很多case了:))。
当然还有一种方法是父页面去实现一个接口,即去实现SetLabel方法,uc把this.Page转成这个接口就可以了。
用户控件:
private
void
Button1_Click(
object
sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//用反射方法动态调用父页面的方法
System.Web.UI.Page p = this.Page;
Type pageType = p.GetType();
MethodInfo mi = pageType.GetMethod("SetLabel");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
mi.Invoke(p,new object[]
{"你这个大猪猪!"});
}
父页面:
public
void
SetLabel(
string
str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.Label1.Text = str;;
}
this.Page.GetType().GetMethod("WriteOptLog").Invoke(this.Page,new object[]{"添加新图书(条形码:" + sb.ToString()+")"});
以上语句只有在WriteOptLog()方法无重载时才可用,若此方法有多个重载则应使用以下方法给GetMethod()方法加上第2个参数,如:
此处假设WriteOptLog()方法有三种方式的重载,如下所示:
1、public void WriteOptLog();
2、public void WriteOptLog(string);
3、public void WriteOptLog(string,int);
则对应的三种调用方法如下所示:
1、调用无参数的WriteOptLog()方法
Type[] typeArray
=
new
Type[
0
];
//
创建一个Type类型的数组
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
this
.Page.GetType().GetMethod(
"
WriteOptLog
"
,typeArray).Invoke(
this
.Page,
new
object
[]
{"添加新图书(条形码:" + sb.ToString()+")"}
);
2、调用一个参数的WriteOptLog(string)方法
Type[] typeArray
=
new
Type[
1
];
//
创建一个Type类型的数组
typeArray.SetValue(
typeof
(
string
),
0
);
//
设置指定数组元素的值,此处表示设置typeArray[0]元素的值为string类型值
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
this
.Page.GetType().GetMethod(
"
WriteOptLog
"
,typeArray).Invoke(
this
.Page,
new
object
[]
{"添加新图书(条形码:" + sb.ToString()+")"}
);
3、调用两个参数的WriteOptLog(string,int)方法
Type[] typeArray
=
new
Type[
2
];
//
创建一个Type类型的数组
typeArray.SetValue(
typeof
(
string
),
0
);
//
设置指定数组元素的值,此处表示设置typeArray[0]元素的值为string类型值
typeArray.SetValue(
typeof
(
int
),
1
);
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
this
.Page.GetType().GetMethod(
"
WriteOptLog
"
,typeArray).Invoke(
this
.Page,
new
object
[]
{"添加新图书(条形码:" + sb.ToString()+")"}
);
父页面类型用反射的方法获取,这避免了不同页面调用同样时,需要类型转换的问题(不用写很多case了:))。
当然还有一种方法是父页面去实现一个接口,即去实现SetLabel方法,uc把this.Page转成这个接口就可以了。
用户控件:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
父页面:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
this.Page.GetType().GetMethod("WriteOptLog").Invoke(this.Page,new object[]{"添加新图书(条形码:" + sb.ToString()+")"});
以上语句只有在WriteOptLog()方法无重载时才可用,若此方法有多个重载则应使用以下方法给GetMethod()方法加上第2个参数,如:
此处假设WriteOptLog()方法有三种方式的重载,如下所示:
1、public void WriteOptLog();
2、public void WriteOptLog(string);
3、public void WriteOptLog(string,int);
则对应的三种调用方法如下所示:
1、调用无参数的WriteOptLog()方法
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/None.gif)
2、调用一个参数的WriteOptLog(string)方法
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/None.gif)