简体   繁体   中英

How to bind a gridview to the result of UserControl?

My aspx page contains a user control and a GridView. When a button within the user control is clicked, a DataTable is created, I need to set the GridView datasource in my aspx page to that DataTable.

I read about "RaiseBubbleEvent" method to pass the event of Clicking the button to the parent page, but I don't need to just pass the event, I need to pass the created DataTable as well.

This is my User Control:

<table>
    <tr>
        <td style="width: 10%">
            <asp:Label ID="lblSearch" runat="server" Text="Search" Width="50px" Font-Size="Medium"></asp:Label>
        </td>

        <td style="width: 10%">
            <asp:DropDownList CssClass="myddl" ID="DDLSearch" runat="server" Width="100px" OnSelectedIndexChanged="DDLRefugeeSearch_SelectedIndexChanged">
            </asp:DropDownList>
        </td>
        <td style="width: 10%">
            <asp:TextBox CssClass="mytextbox" ID="txtSearch" runat="server"></asp:TextBox>
        </td>
        <td style="width: 10%">
            <asp:Button ID="BtnGo" runat="server" Text="Go" OnClick="getSearchResults" />

        </td>
    </tr>


</table>

This is the code behind for "getSearchResult" event:

protected DataTable getSearchResults(object sender, EventArgs e)
        {
            string FieldName=DDLSearch.SelectedValue;
            string SearchText=txtSearch.Text.Replace(" ","");

            RaiseBubbleEvent(this, e);
            return _BLTablesSearch.getSearchResults(FieldName, SearchText);
        }

This is the UserControl withing my page:

    <td>
           <uc1:QuickSearchControl runat="server" id="QuickSearchControl" />
    </td>

So my question is how can I do the following:

1- When the Go button of the user control is clicked, raise an event in the parent page
2- Perform the required operation in the user control
3- return the datatable to the parent page
4- bind the GridView to that datatable

EDIT:

I updated my code as follows following the answer of @Lior Raz

This is the user control code:

public string TableName;
    BLTablesSearch _BLTablesSearch = new BLTablesSearch();
    public delegate void SearchComplete(DataTable FilteredData);
    public event SearchComplete OnSearchComplete;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DDLSearch.DataSource = _BLTablesSearch.getSearchFields(TableName);
            DDLSearch.DataTextField = "FieldText";
            DDLSearch.DataValueField = "FieldValue";
            DDLSearch.DataBind();
        }
    }

    protected void getSearchResults(object sender, EventArgs e)
    {
        string FieldName=DDLSearch.SelectedValue;
        string SearchText=txtSearch.Text.Replace(" ","");



    }

This is the aspx page code:

    BLPerson BLPerson = new BLPerson();
    BLREOptions BLREOptions = new BLREOptions();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PersonListGridView.DataSource = BLPerson.getAllPersons();
            PersonListGridView.DataBind();
            QuickSearchControl.TableName = "Person";

        }
        QuickSearchControl.OnSearchComplete += new UserControls.QuickSearchControl.SearchComplete(HandleOnSearchComplete);

    }

    public void HandleOnSearchComplete(DataTable _searchResult)
    {

    }

don't use RaiseBubbleEvent i think it's a poor design choice since it could lead to cluttered code when you have many child controls using this Bubbling approach, use delegation and events to control the flow of your program instead.

in your UserControl add these lines at your class declaration:

public delegate void SearchComplete(DataTable FilteredData);
public event SearchComplete OnSearchComplete;

also, have a property that holds the original DataTable (so when you clear the search you can show the unfiltered results)

UserControl Code:

public DataTable AllRecords { get { return Session["AllRecords"]; } set { Session["AllRecords"] = value;} }
public string TableName;
BLTablesSearch _BLTablesSearch = new BLTablesSearch();
public delegate void SearchComplete(DataTable FilteredData);
public event SearchComplete OnSearchComplete;

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DDLSearch.DataSource = _BLTablesSearch.getSearchFields(TableName);
        DDLSearch.DataTextField = "FieldText";
        DDLSearch.DataValueField = "FieldValue";
        DDLSearch.DataBind();
    }
}



protected void getSearchResults(object sender, EventArgs e)
{
    string FieldName=DDLSearch.SelectedValue;
    string SearchText=txtSearch.Text.Replace(" ","");

    // TODO: filter the AllRecords Table according to what ever you want
    DataTable filteredRecords = YourFilteringFunction(FieldName,SearchText);

    if(OnSearchComplete != null)
        OnSearchComplete(filteredRecords);
}

On your ASPX page just assign the DataTable to the UserControl Property (that holds the DataTable) and register to the UserControl's event.

In the registered function assign the DataSource of the grid to the FilteredData and call the Bind function of the grid.

ASPX Code:

BLPerson BLPerson = new BLPerson();
BLREOptions BLREOptions = new BLREOptions();

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DataTable allPersons = BLPerson.getAllPersons();
        QuickSearchControl.AllRecords = allPersons;
        PersonListGridView.DataSource = allPersons;
        PersonListGridView.DataBind();
        QuickSearchControl.TableName = "Person";

    }
    QuickSearchControl.OnSearchComplete += new UserControls.QuickSearchControl.SearchComplete(HandleOnSearchComplete);

}

public void HandleOnSearchComplete(DataTable _searchResult)
{
        PersonListGridView.DataSource = _searchResult;
        PersonListGridView.DataBind();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM