2013-09-26 40 views
0

我在我的項目中有這個模塊,其中有2個gridviews。一個用於Main MenuModule,另一個用於子菜單。我創建了一個List,這樣當我的Main Menu Module上的一行被選中並且它有一個對應的子菜單時,它將顯示在SubMenu Gridview上。維護我在GridView複選框上的選擇

現在,我可以看到我的SubMenuGridview,當我回到那個頁面(我使用會話),但我注意到我勾選的複選框都沒有了。

我的問題是我的網頁如何記住我檢查過的複選框,這兩個都來自我的主菜單模塊gridview和我的子菜單gridview。

protected void cbxSelect_CheckedChanged(object sender, EventArgs e) 
{ 
    SubMenuGrid.DataSource = null; 
    SubMenuGrid.DataBind(); 

    Business.SubMenuModules sub = new Business.SubMenuModules(); 

    List<oSubList> oList = new List<oSubList>(); 

    int counter = 0; 

    foreach (GridViewRow nRow in gvModuleList.Rows) 
    { 
     Int32 intModID = Convert.ToInt32(nRow.Cells[0].Text); 
     CheckBox chkBx = (CheckBox)nRow.FindControl("cbxSelect"); 

     if (chkBx.Checked == true) 
     { 
      counter = counter + 1; 

      var oModList = sub.GetAllMenuPerModuleID(intModID); 

      if (oModList.Count > 0) 
      { 
       foreach (var rec in oModList) 
       { 
        oSubList olist = new oSubList 
        { 
         ID = rec.ID, 
         ModuleID = rec.ModuleID, 
         Submenu = rec.Submenu, 
         Description = rec.Description 
        }; 
        oList.Add(olist);  
       } 

       Session["list"]=oList; 

       SubMenuGrid.DataSource = oList; 
       SubMenuGrid.DataBind(); 
      } 
     } 
    } 
} 

回答

0

這是可以做到用:

  1. 的ViewState
  2. SessionPageStatePersister
  3. 自定義基於會話的解決方案

對於視圖狀態 - 見註釋中張貼下面鏈接..

基於定製會話的解決方案

我們將使用預渲染方法。 該方法在頁面初始化之後但在保存其ViewState並呈現之前被調用。 正在將Request.Form加載到會話變量中,並在每次調用不是回發的頁面時加載它。

protected void Page_PreRender(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack && Session["PageState"] != null) 
    { 
    NameValueCollection formValues = (NameValueCollection)Session["PageState"]; 

    String[] keysArray = formValues.AllKeys; 
    for (int i = 0; i < keysArray.Length; i++) 
    { 
     Control currentControl = Page.FindControl(keysArray[i]); 
     if (currentControl != null) 
     { 
     if (currentControl.GetType() == typeof(System.Web.UI.WebControls.TextBox)) ((TextBox)currentControl).Text = formValues[keysArray[i]]; 
     else if (currentControl.GetType() == typeof(System.Web.UI.WebControls.CheckBox)) 
     { 
     if (formValues[keysArray[i]].Equals("on")) ((CheckBox)currentControl).Checked = true; 
     } 
     else if (currentControl.GetType() == typeof(System.Web.UI.WebControls.DropDownList)) 
((DropDownList)currentControl).SelectedValue = formValues[keysArray[i]].Trim(); 
     } 
    } 
    } 

    if(Page.IsPostBack) Session["PageState"] = Request.Form; 

} 

SessionPageStatePersister

抽象PageStatePersister類表示封裝當頁面的狀態的存儲和處理的基類。 存儲可以使用默認的HiddenFieldPageStatePersister類或SessionPageStatePersister來完成。 使用SessionPageStatePersister時,.NET Server管理會話對象中的_VIEWSTATE而不是表單上的隱藏字段。 更改狀態存儲如下所示:

protected override PageStatePersister PageStatePersister 
{ 
    get 
    { 
     return new SessionPageStatePersister(this); 
    } 
} 
+0

如何將Viewstate實現到我的模塊?我從哪開始呢?我可以在Viewstate上看到一些資源,但我無法將其應用於我的場景。 – theNoobie

+0

與此類似.. http://www.aspdotnet-suresh.com/2012/11/viewstate-in-aspnet-with-example-in-c.html – Sasidharan