欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-31 20:30:40
现在把此web用户控件添加到一.aspx页面中.要实现单击用户控件中的button控件把搜索出来的结果数据绑定到.aspx页面的gridview控件上去,如何实现呢? 如果...
现在把此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});
}