[英]Filtering results in ASP.NET Web Forms
I'm trying to walk through a tutorial on web forms, and my attempts to filter search results isn't working quite right: 我正在尝试浏览有关Web表单的教程,而我尝试过滤搜索结果的方法却不太正确:
The contents of the asp page look like this: ASP页面的内容如下所示:
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="button1" runat="server" onclick="button1_Click" Text="Submit" />
<asp:GridView ID="GridView1" AllowPaging="true" PageSize="8" AutoGenerateColumns="false" runat="server"
OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField HeaderText="Qual ID" DataField="ID" />
<asp:BoundField HeaderText="Client Name" DataField="Client_Name" />
<asp:BoundField HeaderText="Project" DataField="Project_Name" />
<asp:BoundField HeaderText="Uploaded By" DataField="Uploaded_By" />
</Columns>
</asp:GridView>
</form>
And the code behind file: 以及文件后面的代码:
public partial class Sample1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SourceDataContext db = new SourceDataContext();
GridView1.DataSource = from q in db.Cust
orderby q.ID
select q;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void button1_Click(object sender, EventArgs e)
{
string client = TextBox1.Text;
SourceDataContext db = new SourceDataContext();
GridView1.DataSource = from q in db.Cust
where q.Client_Name == client
orderby q.ID
select q;
GridView1.DataBind();
}
}
The filtering works, although it the paging stops working. 尽管分页停止工作,但过滤仍然有效。 Any suggestions appreciated.
任何建议表示赞赏。
Thanks. 谢谢。
it seems to me that there are two problems 在我看来,有两个问题
Page_Load
enclose binding in !IsPostBack
Page_Load
上将绑定包含在!IsPostBack
GridView1_PageIndexChanging
do not bind again GridView1_PageIndexChanging
不再绑定 Chris Gessler
Chris Gessler
的收获 Code 码
public partial class Sample1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
SourceDataContext db = new SourceDataContext();
GridView1.DataSource = from q in db.Cust
orderby q.ID
select q;
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
protected void button1_Click(object sender, EventArgs e)
{
string client = TextBox1.Text;
SourceDataContext db = new SourceDataContext();
GridView1.DataSource = from q in db.Cust
where q.Client_Name == client
orderby q.ID
select q;
GridView1.DataBind();
GridView1.PageIndex = 0;
}
}
Issue 1: This should only run on GET requests. 问题1:此方法仅应在GET请求上运行。 ViewState will take over on PostBacks and populate the grid.
ViewState将接管PostBacks并填充网格。
protected void Page_Load(object sender, EventArgs e)
{
if(!this.Page.IsPostback)
{
SourceDataContext db = new SourceDataContext();
GridView1.DataSource = from q in db.Cust
orderby q.ID
select q;
GridView1.DataBind();
}
}
Issue 2: You need to reset the page index because the recordset changed. 问题2:您需要重置页索引,因为记录集已更改。 The current page may not exist.
当前页面可能不存在。
protected void button1_Click(object sender, EventArgs e)
{
string client = TextBox1.Text;
SourceDataContext db = new SourceDataContext();
GridView1.DataSource = from q in db.Cust
where q.Client_Name == client
orderby q.ID
select q;
GridView1.DataBind();
GridView1.PageIndex = 0;
}
Issue 3: You're calling DataBind(), but not setting a new datasource. 问题3:您正在调用DataBind(),但未设置新的数据源。
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
Also, 也,
consider caching the recordset and filtering that instead of making a call to the server for a new recordset, however, this depends on "need". 考虑缓存记录集并进行过滤,而不是为新的记录集调用服务器,但这取决于“需要”。 Caching a recordset will not find any new records obviously, which may not fit your business need.
缓存记录集显然不会找到任何新记录,这可能不适合您的业务需求。
consider setting your events in Code Behind in the OnInit method: 考虑在OnInit方法的“隐藏代码”中设置事件:
protected void Page_Init(object sender, EventArgs e)
{
this.GridView1.PageIndexChanging += GridView1_PageIndexChanging;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.