asp usercontrol插入和绑定网格 [英] asp usercontrol insert and bind grid

查看:44
本文介绍了asp usercontrol插入和绑定网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有标签,文本框和按钮的用户控件,该控件的功能是当我单击按钮时,它应根据文本框中的值进行搜索并在gridview中显示内容.谁能建议我如何在用户控制按钮单击时将数据表返回并将其绑定到.aspx页面上的gridview

I have a user control with a label,textbox and a button, the function of this control is when i clicks the button it should search according to the value in the textboxa and display the contents in the gridview. Can anyone suggest how i can return datatable when user control button click and bind that datatable to gridview which is at .aspx page

推荐答案

您有两个选择. br/>
选项1-将用户控件中的搜索点击事件冒泡到父页面以使用该操作
处理搜索按钮单击事件或命令事件.
在用户控件中声明一个按钮单击事件,即使在父页面中也要处理.
还要在用户控件中声明一个属性以获取,在用户控件文本框中设置搜索值.
然后,当它收到单击事件时,可以从父页面访问此属性.

用户控制:
You have a couple of options.

Option 1 - Bubble up the search clicked event in the user control to the parent page to consume the action
Handle the search button clicked event, or on command event.
Declare a button clicked event in the user control, and handle that even in the parent page.
Also declare a property in the user control to get,set the search value in the user controls textbox.
Then access this property from the parent page when it receives the clicked event.

User control:
public partial class MyUserControl
{
  public String SearchCriteria
  {
    get{ return searchTextBox.Text; }
  }

  public event EventHandler SearchClicked;

  protected void searchButton_Clicked(object sender, EventArgs e)
  {
    if(SearchClicked!=null)
    {
      SearchClicked(sender,e);
    }
  }
}



父页面:
假设MyUserControl在父页面上以"myUserControl"的形式存在.



Parent page:
Lets assume MyUserControl exists on parent page as ''myUserControl''.

protected void Page_Load(object sender, EventArgs e)
{
  // Handle the search click event
  myUserControl.SearchClicked += new EventHandler(myUserControl_SearchClicked);
}

protected void myUserControl_SearchClicked(object sender, EventArgs e)
{
  // Put your search logic in here using myUserControl.SearchCriteria
}



选项2-在用户控件内执行所有搜索功能



Option 2 - Do all the search functionality inside the user control

public partial class MyUserControl
{
  public DataTable SearchResult
  {
    get
    {
      if(Session["searchResult"] == null)
      {
        Session["searchResult"] = new DataTable;
      }
      return Session["searchResult"];
    }
    set
    { Session["searchResult"] = value; }
  }

  public event EventHandler SearchClicked;

  protected void searchButton_Clicked(object sender, EventArgs e)
  {
    // Put your search logic in here and put the result in session variable SearchResult
    if(SearchClicked!=null)
    {
      // This is to allow the parent page to receive the event after the user control builds the search result data.
      SearchClicked(sender,e);
    }
  }
}


这篇关于asp usercontrol插入和绑定网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆