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

  现在把此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});

  }