0

我正在尝试浏览有关 Web 表单的教程,但过滤搜索结果的尝试并不正确:

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>

以及文件背后的代码:

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

}
}

过滤工作,尽管分页停止工作。任何建议表示赞赏。

谢谢。

4

2 回答 2

2

在我看来有两个问题

  1. Page_Load附上绑定在!IsPostBack
  2. GridView1_PageIndexChanging不要再次绑定
  3. 看问题Chris Gessler

代码

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;

}
}
于 2012-07-06T11:17:41.340 回答
1

问题 1:这应该只在 GET 请求上运行。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(); 
    }
}

问题 2:您需要重置页面索引,因为记录集已更改。当前页面可能不存在。

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;

}   

问题 3:您正在调用 DataBind(),但没有设置新的数据源。

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)              
{              
    GridView1.PageIndex = e.NewPageIndex;                         
} 

还,

考虑缓存记录集并过滤它,而不是调用服务器以获取新记录集,但是,这取决于“需要”。缓存记录集显然不会找到任何新记录,这可能不适合您的业务需求。

考虑在 OnInit 方法的代码隐藏中设置您的事件:

protected void Page_Init(object sender, EventArgs e) 
{
    this.GridView1.PageIndexChanging += GridView1_PageIndexChanging;
}
于 2012-07-06T11:22:04.303 回答