过滤ASP.NET Web窗体中的结果

时间:2012-07-06 11:07:29

标签: c# asp.net linq linq-to-sql webforms

我正在尝试浏览网络表单上的教程,而我尝试过滤搜索结果的效果并不正常:

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

}
}

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

感谢。

2 个答案:

答案 0 :(得分:2)

在我看来,有两个问题

  1. on Page_Load!IsPostBack
  2. 中的绑定括起来
  3. GridView1_PageIndexChanging不再绑定
  4. Chris Gessler
  5. 查看

    代码

    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;
    
    }
    }
    

答案 1 :(得分:1)

问题1:这应该仅在GET请求上运行。 ViewState将接管PostBack并填充网格。

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方法中在Code Behind中设置事件:

protected void Page_Init(object sender, EventArgs e) 
{
    this.GridView1.PageIndexChanging += GridView1_PageIndexChanging;
}