我的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-单击用户控件的Go按钮时,在父页面中引发事件
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)
{
}
答案 0 :(得分:1)
不要使用RaiseBubbleEvent我认为这是一个糟糕的设计选择,因为当你有许多使用这种Bubbling方法的子控件时,它会导致混乱的代码,使用委托和事件来控制程序的流程。
在您的UserControl中在您的类声明中添加以下行:
public delegate void SearchComplete(DataTable FilteredData);
public event SearchComplete OnSearchComplete;
另外,有一个保存原始DataTable的属性(所以当你清除搜索时,你可以显示未经过滤的结果)
UserControl代码:
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的活动。
在注册函数中,将网格的DataSource分配给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();
}