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