启用分页的 GridView 不会根据其他控件(DropDownList、TextBox)的输入进行过滤
有人可以帮助我吗? 我在页面上动态创建了控件,其中包含 GridView、DropDownList、TextBox 和 Button。
我已成功将数据检索到 GridView 控件上,然后启用了分页属性。现在,我尝试根据用户通过 DropDownList 和按钮单击事件触发的 TextBox 设置的输入来过滤数据。然而,这不起作用。
我有以下代码:
在CreateChildControls中:
Controls.Add(_searchTitle);
Controls.Add(_searchDDL);
Controls.Add(_searchTextBox);
Controls.Add(_searchBtn);
Controls.Add(new LiteralControl("<br /><br />"));
Controls.Add(_title);
Controls.Add(new LiteralControl("<br /><br />"));
SelectEmployees(_searchDDL, _searchTextBox, _grid, _empObj, _dt, _strConn);
EmployeesGrid(_grid, _IDColumn, _hyperlinkedColumn, _column, _title);
Controls.Add(_grid);
DesignGrid(_grid);
if(!Page.IsPostBack)
{
SearchArea(_searchTitle, _searchDDL, _searchTextBox, _searchBtn);
}
SelectEmployees方法负责查询数据。 EmployeesGrid 仅将查询呈现到 GridView 控件上。 DesignGrid 是定义网格样式的方法。 SearchArea 定义搜索控件的样式。
单击按钮时将调用以下代码。然而,它没有做任何事情:
private void FilterEmployees(DropDownList searchDDL, TextBox searchTextBox, GridView grid, DataTable dt)
{
string col = "";
if ((searchDDL.SelectedValue.Equals("First Name")) && (!searchTextBox.Text.Equals("")))
{
col = "FirstName = " + searchTextBox.Text;
}
else if ((searchDDL.SelectedValue.Equals("Last Name")) && (!searchTextBox.Text.Equals("")))
{
col = "LastName" + searchTextBox.Text;
}
else if ((searchDDL.SelectedValue.Equals("Department")) && (!searchTextBox.Text.Equals("")))
{
col = "Department" + searchTextBox.Text;
}
else if ((searchDDL.SelectedValue.Equals("ID Number")) && (!searchTextBox.Text.Equals("")))
{
col = "IDNumber" + searchTextBox.Text;
}
else
{
}
DataView view = new DataView(dt);
view.RowFilter = col;
grid.DataSource = view;
DesignGrid(grid);
}
请帮忙。谢谢。
Can someone please assist me.
I have dynamically created controls onto my page which consists of a GridView, DropDownList, TextBox and Button.
I have successfully retrieved data onto the GridView control which then has a paging attribute enabled. Now I am trying to filter data according to the input set by the user through the DropDownList and the TextBox fired by the Button click event. However, it doesn't work.
I have the following code:
In CreateChildControls:
Controls.Add(_searchTitle);
Controls.Add(_searchDDL);
Controls.Add(_searchTextBox);
Controls.Add(_searchBtn);
Controls.Add(new LiteralControl("<br /><br />"));
Controls.Add(_title);
Controls.Add(new LiteralControl("<br /><br />"));
SelectEmployees(_searchDDL, _searchTextBox, _grid, _empObj, _dt, _strConn);
EmployeesGrid(_grid, _IDColumn, _hyperlinkedColumn, _column, _title);
Controls.Add(_grid);
DesignGrid(_grid);
if(!Page.IsPostBack)
{
SearchArea(_searchTitle, _searchDDL, _searchTextBox, _searchBtn);
}
SelectEmployees method is responsible for querying the data.
EmployeesGrid only renders the query onto the GridView control.
DesignGrid is the method that defines the style for the grid.
SearchArea defines the style for the search controls.
The following code is called upon button click. However, it doesn't do anything:
private void FilterEmployees(DropDownList searchDDL, TextBox searchTextBox, GridView grid, DataTable dt)
{
string col = "";
if ((searchDDL.SelectedValue.Equals("First Name")) && (!searchTextBox.Text.Equals("")))
{
col = "FirstName = " + searchTextBox.Text;
}
else if ((searchDDL.SelectedValue.Equals("Last Name")) && (!searchTextBox.Text.Equals("")))
{
col = "LastName" + searchTextBox.Text;
}
else if ((searchDDL.SelectedValue.Equals("Department")) && (!searchTextBox.Text.Equals("")))
{
col = "Department" + searchTextBox.Text;
}
else if ((searchDDL.SelectedValue.Equals("ID Number")) && (!searchTextBox.Text.Equals("")))
{
col = "IDNumber" + searchTextBox.Text;
}
else
{
}
DataView view = new DataView(dt);
view.RowFilter = col;
grid.DataSource = view;
DesignGrid(grid);
}
Please help. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对不起。我在这里可能很傻。难道是因为没有调用“grid.DataBind()”方法?
我刚刚尝试了一个非常相似的程序,并且能够按照您的预期进行过滤。
-Prasanna K Rao
Sorry. I could be very silly here. Could it be because there is no call to the "grid.DataBind()" method?
I just tried a very similar program and I was able to filter as you expect.
-Prasanna K Rao
您确定要使用正确的选定值在回发时重新添加控件吗? (您需要将控件添加到会话中并在每次回发时加载它们)
如果您这样做,您能否向我们展示将 onclick 事件绑定到按钮的位置。
Are you sure that you are re-adding your controls on postback with the correct selected values? (you need to add your controls to session and load them everytime on postback)
If you are doing so, can you show us where you bind the onclick event to your button.