您的位置:首页 > 编程语言 > ASP

web用户控件调用.aspx页面里的方法

2018-10-12 14:07 573 查看
现在把此web用户控件添加到一.aspx页面中.要实现单击用户控件中的button控件把搜索出来的结果数据绑定到.aspx页面的gridview控件上去,如何实现呢?
如果gridview控件是放在.ascx文件中的话,那我们直接把搜索出来的数据绑定到它上面就行了。但现在gridview是放在.aspx文件里,也就是说web用户控件要如何才能访问母页面的控件,把数据绑定到母页面的控件上去?
解决方法:
1.先在.aspx页面的后台文件.aspx.cs中添加一个绑定数据的方法,代码如下:

public void BindSearchDataToGridView(string ddlvalue,string txtValue)
{
//ddlvalue 为用户控件中dropdownlist控件的值
//txtValue 为用户控件中textbox控件的值
//通过传进来的参数去查询数据,然后绑定到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("BindSearchDataToGridView"); //"BindSearchDataToGridView"为.aspx页面文件的方法
string txtValue= TextBox1.Text;
string ddlvalue= DropDownList1.SelectedValue.ToString();
mi.Invoke(motherPage, new object[] { ddlvalue, txtValue});
}

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐