现在把此web用户控件添加到一.aspx页面中.要实现单击用户控件中的button控件把搜索出来的结果数据绑定到.aspx页面的gridview控件上去,如何实现呢?
如果gridview控件是放在.ascx文件中的话,那我们直接把搜索出来的数据绑定到它上面就行了。但现在gridview是放在.aspx文件里,也就是说web用户控件要如何才能访问母页面的控件,把数据绑定到母页面的控件上去?
解决方法:
首先,在.aspx页面的后台文件.aspx.cs中写一个绑定数据的方法
public void BindGridView(string a,string b,...)
{
//a,b,...为用户控件中过来的查询参数
//通过传进来的参数去查询数据,然后绑定到gridview控件上
}
2.在web用户控件中实现button控件的方法代码如下:
protected void btnSearch_Click(object sender, EventArgs e)
{
System.Web.UI.Page motherPage = this.Page;
Type pageType = motherPage.GetType();
//这里用反射
System.Reflection.MethodInfo mi = pageType.GetMethod("BindGridView"); //"BindGridView"为.aspx页面上写的方法
string c = 用户控件上的其他控件的查询参数1;
string d =用户控件上的其他控件的查询参数2;
mi.Invoke(motherPage, new object[] {c,d,...}); //搞定
}
mi.Invoke(motherPage, new object[] {c,d,...}); //搞定
}