网格视图索引随过滤器变化

本文关键字:过滤器 变化 索引 视图 网格 | 更新日期: 2023-09-27 18:04:38

我得到这个简单的gridview

<asp:GridView ID="GridViewFoundations" runat="server" AutoGenerateColumns="False"  
          Width="100%" AllowPaging="True" AllowSorting="True" PageSize="15" 
          CellPadding="4" ForeColor="#333333" GridLines="None" 
          onpageindexchanging="GridViewFoundations_PageIndexChanging">
      <AlternatingRowStyle BackColor="White" />
  <Columns>
  <asp:TemplateField HeaderText="Id">
  <ItemTemplate><asp:Label ID="lb_id" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "nodeId") %>'></asp:Label></ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Foundation Name">
  <ItemTemplate><asp:Label ID="lb_foundationName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "text") %>'></asp:Label></ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="LastUpdate">
  <ItemTemplate><asp:Label ID="lb_lastUpdate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "updateDate") %>'></asp:Label></ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Expire Date">
  <ItemTemplate><asp:Label ID="lb_expireDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "expireDate") %>'></asp:Label></ItemTemplate>
  </asp:TemplateField>
  </Columns>
      <EditRowStyle BackColor="#2461BF" />
      <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
      <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
      <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
      <RowStyle BackColor="#EFF3FB" />
      <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
      <SortedAscendingCellStyle BackColor="#F5F7FB" />
      <SortedAscendingHeaderStyle BackColor="#6D95E1" />
      <SortedDescendingCellStyle BackColor="#E9EBEF" />
      <SortedDescendingHeaderStyle BackColor="#4870BE" />
  </asp:GridView>

然后我将它绑定到页面加载事件上,像这样

protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }
        protected void BindData()
        {
            string sqlConnectString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ToString();
            string sqlSelect = "SELECT  cmsContentXml.nodeId,text, Max(updateDate) as UpdateDate,expireDate as ExpireDate from cmsContentXml,cmsDocument,cmsContent where cmsContent.nodeId=cmsContentXml.nodeId and cmsDocument.nodeId=cmsContent.nodeId  group by cmsContentXml.nodeId,text,expireDate";
            SqlConnection sqlConnection = new SqlConnection(sqlConnectString);
            SqlCommand sqlCommand = new SqlCommand(sqlSelect, sqlConnection);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCommand);
            DataTable sqlDt = new DataTable();
            sqlDa.Fill(sqlDt);
            GridViewFoundations.DataSource = sqlDt;
            GridViewFoundations.DataBind();
        }

我有以下过滤器(例如通过文本)

protected void btn_filtro_Click(object sender, EventArgs e)
        {
            string sqlConnectString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ToString();
            string sqlSelect = "SELECT  cmsContentXml.nodeId,text, Max(updateDate) as UpdateDate,expireDate as ExpireDate from cmsContentXml,cmsDocument,cmsContent where cmsContent.nodeId=cmsContentXml.nodeId and cmsDocument.nodeId=cmsContent.nodeId  and text like '%" + TextBox1.Text + "%' group by cmsContentXml.nodeId,text,expireDate";
            SqlConnection sqlConnection = new SqlConnection(sqlConnectString);
            SqlCommand sqlCommand = new SqlCommand(sqlSelect, sqlConnection);

            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCommand);
            DataTable sqlDt = new DataTable();
            sqlDa.Fill(sqlDt);
            GridViewFoundations.DataSource = sqlDt;
            GridViewFoundations.DataBind();
        }

我的问题是我不能改变页面的索引和保持我的过滤器…

我已经试过了

GridViewFoundations.PageIndex = e.NewPageIndex

后接

gridviewFoundations.Databind()BindData()

但是在第一种情况下,gridview消失了,在第二种情况下,它清除了所有的过滤器(显然)。

所以谁能帮我改变与过滤器网格的页面?

网格视图索引随过滤器变化

在第一种情况下(GridViewFoundations.PageIndex = e.NewPageIndex followed by gridviewFoundations.Databind())数据消失,因为您没有提供任何数据源来重新发送后的网格。

在第二种情况下(BindData()),你绑定网格没有任何过滤器,因此你的过滤器丢失。

你能做的就是创建一个新的函数

protected void BindFilteredData()
 {
            string sqlConnectString = ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ToString();
            string sqlSelect = "SELECT  cmsContentXml.nodeId,text, Max(updateDate) as UpdateDate,expireDate as ExpireDate from cmsContentXml,cmsDocument,cmsContent where cmsContent.nodeId=cmsContentXml.nodeId and cmsDocument.nodeId=cmsContent.nodeId  and text like '%" + TextBox1.Text + "%' group by cmsContentXml.nodeId,text,expireDate";
            SqlConnection sqlConnection = new SqlConnection(sqlConnectString);
            SqlCommand sqlCommand = new SqlCommand(sqlSelect, sqlConnection);

            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCommand);
            DataTable sqlDt = new DataTable();
            sqlDa.Fill(sqlDt);
            GridViewFoundations.DataSource = sqlDt;
            GridViewFoundations.DataBind();
}

在页面加载

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
     {
       BindData();
     }
      else
        BindFilteredData();
}

这将在你的页面第一次加载时调用BindData,其余时间它将调用filtered data函数