Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 6-Nov-12 11:46am
v2

1 solution

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:
C#
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);
    }
  }
}


Parent page:
Lets assume MyUserControl exists on parent page as 'myUserControl'.
C#
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
}


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

C#
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);
    }
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900