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

时间:2014-05-22 08:40:16

标签: c# asp.net gridview

我有这个简单的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>

然后我在页面加载事件上绑定if,就像这样

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会消失,而在第二种情况下,它会清除所有过滤器(显然)。

那么任何人都可以帮助我用过滤器更改网格页面吗?

1 个答案:

答案 0 :(得分:2)

在第一种情况下(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,其余时间将调用过滤数据函数。