【问题标题】:Using DropDown_SelectedIndexChanged to bind gridview, but paging is not working使用DropDown_SelectedIndexChanged绑定gridview,但分页不起作用
【发布时间】:2012-07-30 06:56:04
【问题描述】:

我有一个 DropDownList 列表项很少。每次我选择任何项目时,我都会基于它绑定一个网格视图,因此生成的网格视图取决于我的选择。一些结果超出了页面大小,因此我需要在 GridView 的第二页上导航,但是当我选择第二页时,GridView 正在消失。

 protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
 {
    string constr = ConfigurationSettings.AppSettings["ConnectionInfo"];
    SqlConnection con = new SqlConnection(constr);

    con.Open();
    string str = string.Empty;
    str = "SELECT * FROM Table1";

    SqlDataAdapter adp = new SqlDataAdapter(str, con);

    DataSet ds = new DataSet();
    adp.Fill(ds, "myds");

    BusinessKPIGrid.DataSource = ds;
    BusinessKPIGrid.DataBind();
}

 protected void OnGridViewPaging(object sender, GridViewPageEventArgs e)
 {
    //I know I need to bind the gridview here, but how ?
    BusinessKPIGrid.PageIndex = e.NewPageIndex;
    BusinessKPIGrid.DataBind();
 }

我不知道如何在分页事件中绑定gridview,因为我没有任何单独的绑定函数。

【问题讨论】:

    标签: asp.net drop-down-menu paging selectedindexchanged


    【解决方案1】:

    网格视图的页面事件有一个单独的函数。创建单独的方法来绑定网格并在 pageindexchange 事件上调用该方法..

    protected void BusinessKPIGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        BusinessKPIGrid.PageIndex = e.NewPageIndex; 
         bindyourgrid();
    }
    

    【讨论】:

    【解决方案2】:

    试试这个

    protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        string constr = ConfigurationSettings.AppSettings["ConnectionInfo"];
        SqlConnection con = new SqlConnection(constr);
    
        con.Open();
        string str = string.Empty;
        str = "SELECT * FROM Table1";
    
        SqlDataAdapter adp = new SqlDataAdapter(str, con);
    
        DataSet ds = new DataSet();
        adp.Fill(ds, "myds");
        ViewState["ds"]=ds;
        BusinessKPIGrid.DataSource = ds;
        BusinessKPIGrid.DataBind();
    
    }
    
      protected void OnGridViewPaging(object sender, GridViewPageEventArgs e)
        {
            //I know I need to bind the gridview here, but how ?
            BusinessKPIGrid.PageIndex = e.NewPageIndex;
             BusinessKPIGrid.DataSource = (DataSet)ViewState["ds"];
            BusinessKPIGrid.DataBind();
        }
    

    【讨论】:

      【解决方案3】:

      从 DDL_SelectedIndexChanged 如果您正在搜索某些内容,则使用“PageIndexChanging”并为其设置页面大小

      protected void gvworker_PageIndexChanging(object sender, GridViewPageEventArgs e)
          {
              gvworker.PageIndex = e.NewPageIndex;
              //result got from ddl list i.e bind your data set
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-03
        • 2010-10-12
        • 1970-01-01
        • 1970-01-01
        • 2012-09-02
        相关资源
        最近更新 更多