2012-07-30 47 views
0

我有一個DropDownList它有少數列表項。每次我選擇任何項目,基於它我綁定一個GridView,因此結果GridView取決於我的選擇。一些結果超出頁面大小,因此我需要在GridView的第二頁導航,但是當我選擇第二頁時,GridView正在消失。使用DropDown_SelectedIndexChanged綁定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,因爲我沒有任何單獨的綁定函數。

回答

1

存在對於電網頁面事件的獨立功能view..create結合電網&調用該方法上pageindexchange事件..

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

Thnx全部。我在這裏得到了解決方案: http://forums.asp.net/t/1828875.aspx/1?Using+DropDown_SelectedIndexChanged+to+bind+gridview+but+paging+is+not+working – 2012-07-30 07:23:27

1

單獨的方法試試這個

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(); 
    } 
0

從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 
    }