0

我正在尝试过滤 GridView 的内容,但是在此过程中出现了一些异常行为:

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="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)
  {
    if (!this.Page.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();

    //reset page index to 0
    GridView1.PageIndex = 0;
}
}

过滤部分有效,但分页无效。此外,分页需要单击该页面两次才能被选中。

谢谢。

4

1 回答 1

0

试试看。将此代码保存在一个函数中,将其命名为 BindGrid

private void BindGrid()
{
 SourceDataContext db = new SourceDataContext();

    GridView1.DataSource = from q in db.Cust
                           orderby q.ID
                           select q;
    GridView1.DataBind();
}
// Create this method in your code behind

并在分配新索引后在 GridView1_PageIndexChanging 中调用它

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    BindGrid();
}
于 2012-07-16T12:35:24.290 回答