ViewState to compare CheckBoxList Selected Values

本文关键字:Selected Values CheckBoxList compare to ViewState | 更新日期: 2023-09-27 17:51:11

我有一个按钮,创建一个CheckBoxListCheckBoxList中的项目是根据用户当前的通知订阅来选择的。我正在存储所选项目的真或假值。

用户通过勾选或取消勾选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
        }

ViewState to compare CheckBoxList Selected Values

你可以试试这样

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