2013-01-09 48 views
0

我有一個按鈕,它會創建CheckBoxListCheckBoxList中的項目是根據用戶當前的通知訂閱來選擇的。我正在存儲所選項目的真值或假值。ViewState比較CheckBoxList選定值

在用戶通過選中或取消選中CheckBoxList中的框來更改其訂閱之後,他們單擊另一個按鈕來更新其通知訂閱。我需要比較Viewstate["PREV"]與回發中更改的選擇,並根據更改運行一些功能。

ArrayList list = new ArrayList(); 
for (int i = 0; i < checkBoxList1.Items.Count; i++) 
{ 
    list.Add(checkBoxList1.Items[i].Selected.ToString()); 
} 
ViewState["PREV"] = list; 

即使我不能看到一個Response.Write值,當我在調試添加監視的ViewState中的值是否正確。

我遇到的問題是如何接近下一步做比較。這是我想要完成的任務:

  for (int i = 0; i < checkBoxList1.Items.Count; i++) 
     { 
      //if checkbox state goes from false to true, subscribe to topic 
       //mySubscribeMethod 
      //else if checkbox state goes from true to false, unsubscribe to topic 
       //myUnsubscribeMethod 
      //else if checkbox state is unchanged, do nothing 
     } 

回答

0

你可以嘗試這樣的事情

ArrayList list = ViewState["PREV"] as ArrayList; 
for (int i = 0; i < checkBoxList1.Items.Count; i++) 
{ 
    if (checkBoxList1.Items[i].Selected == true && Convert.ToBoolean(list[i]) == false) 
     { 
      // Subscribe Method 
     } 
    if (checkBoxList1.Items[i].Selected == false && Convert.ToBoolean(list[i]) == true)  
     { 
      // Unsubscribe Method 
     } 
    else 
     { 
      // Continue to loop 
     } 
}