当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > web用户控件调用.aspx页面里的方法

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 113 ::
收藏到网摘: n/a

今天在一QQ技术群有朋友问: 他在web用户控件中(.ascx)中放了一个dropdownlist控件,一个textbox控件和一个button控件。 现在把此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});
}