簡體   English   中英

網格視圖索引隨過濾器而變化

[英]Grid view index changing with filters

我有這個簡單的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會消失,而在第二種情況下,它會清除所有過濾器(顯然)。

那么任何人都可以幫助我用過濾器更改網格頁面嗎?

在第一種情況下( 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,其余時間將調用過濾數據函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM