簡體   English   中英

如何將gridview綁定到UserControl的結果?

[英]How to bind a gridview to the result of UserControl?

我的aspx頁面包含一個用戶控件和一個GridView。 單擊用戶控件中的按鈕時,將創建一個DataTable,我需要將aspx頁面中的GridView數據源設置為該DataTable。

我閱讀了有關“ RaiseBubbleEvent”方法的信息,該方法將單擊按鈕的事件傳遞給父頁面,但是我不需要傳遞事件,也需要傳遞創建的DataTable。

這是我的用戶控件:

<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>

這是“ getSearchResult”事件的背后代碼:

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

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

這是伴隨我的頁面的UserControl:

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

所以我的問題是如何執行以下操作:

1-單擊用戶控件的“轉到”按鈕時,在父頁面中引發一個事件
2-在用戶控件中執行所需的操作
3-將數據表返回到父頁面
4-將GridView綁定到該數據表

編輯:

我按照@Lior Raz的答案更新了我的代碼,如下所示

這是用戶控制代碼:

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(" ","");



    }

這是aspx頁面代碼:

    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)
    {

    }

不要使用RaiseBubbleEvent,我認為這是一個糟糕的設計選擇,因為當您使用此冒泡方法的許多子控件時,它可能導致代碼混亂,請改用委托和事件來控制程序的流程。

在您的UserControl中,在類聲明中添加以下行:

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

另外,還有一個保存原始DataTable的屬性(因此,當您清除搜索時,可以顯示未過濾的結果)

用戶控制代碼:

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);
}

在ASPX頁面上,只需將DataTable分配給UserControl屬性(包含DataTable),然后注冊到UserControl的事件。

在已注冊的函數中,將網格的數據源分配給FilteredData並調用網格的Bind函數。

ASPX代碼:

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM