locked
CheckBoxList - which item cause the event RRS feed

  • Question

  • Hi,
    I'm writing a website using ASP.NET and C#.
    I have a CheckBoxList in my site and while selecting an item in the list and firing the SelectedIndexChanged event I want to know which item in the list cause the event (which item was last selected).
    I'll appreciate very much any assistance,
    Amir
    Saturday, October 25, 2008 2:02 PM

Answers

  •   sorry, I didn't think about that. What you need is a way to store global variables. If you can do this, you could use some kind of indexed array storing the index of the cb's and a true/false value if they are checked. then compare the differences. seems like alot of overhead. if there is another way for you to work around this, you may want to consider taking it. here is a quick search I pulled up with some info about storing vars in asp.net

    http://www.codeproject.com/KB/aspnet/datastorage.aspx
    Sunday, October 26, 2008 12:00 PM
  • Hi ros_amir,
       
       Please try the following code:


    1     private static bool[] arrtmp;    
    2  
    3     protected void Page_Load(object sender, EventArgs e)  
    4     {  
    5         if (!IsPostBack)  
    6         {  
    7             arrtmp = new bool[CheckBoxList1.Items.Count];  
    8             for (int i = 0; i <= CheckBoxList1.Items.Count - 1; i++)  
    9             {  
    10                 arrtmp[i] = CheckBoxList1.Items[i].Selected;  
    11             }  
    12         }     
    13     }  
    14  
    15     protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)  
    16     {  
    17         int clicknum;  
    18         clicknum = -1;  
    19         for (int i = 0; i <= CheckBoxList1.Items.Count - 1; i++)  
    20         {  
    21             if (arrtmp[i] != CheckBoxList1.Items[i].Selected)  
    22             {  
    23                 arrtmp[i] = CheckBoxList1.Items[i].Selected;  
    24                 clicknum = i;  
    25                 break;  
    26             }  
    27         }  
    28         if (clicknum >= 0)  
    29         {  
    30             Response.Write(CheckBoxList1.Items[clicknum].ToString() + "   caused the event!");  
    31         }  
    32     }  
    33  

    Hope this will help you.


    Kind regards,
    Guo Surfer

    Tuesday, October 28, 2008 10:32 AM

All replies

  • the form_load handler will fire before the selectedindexchanged, so you could create a class level variable to hold the index of the currently selected checkbox just before it is changed by setting its value in the form_load handler.
    • Edited by BradMS Saturday, October 25, 2008 6:05 PM
    Saturday, October 25, 2008 6:04 PM
  • Thanks for the help, but how can I know the index of the item just been selected (as far as I know I can only retrieve the minimal selected item but I have no way to know the index just fire the event)
    Thanks again,
    Amir
    Saturday, October 25, 2008 10:29 PM
  •   sorry, I didn't think about that. What you need is a way to store global variables. If you can do this, you could use some kind of indexed array storing the index of the cb's and a true/false value if they are checked. then compare the differences. seems like alot of overhead. if there is another way for you to work around this, you may want to consider taking it. here is a quick search I pulled up with some info about storing vars in asp.net

    http://www.codeproject.com/KB/aspnet/datastorage.aspx
    Sunday, October 26, 2008 12:00 PM
  • Hi ros_amir,
       
       Please try the following code:


    1     private static bool[] arrtmp;    
    2  
    3     protected void Page_Load(object sender, EventArgs e)  
    4     {  
    5         if (!IsPostBack)  
    6         {  
    7             arrtmp = new bool[CheckBoxList1.Items.Count];  
    8             for (int i = 0; i <= CheckBoxList1.Items.Count - 1; i++)  
    9             {  
    10                 arrtmp[i] = CheckBoxList1.Items[i].Selected;  
    11             }  
    12         }     
    13     }  
    14  
    15     protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)  
    16     {  
    17         int clicknum;  
    18         clicknum = -1;  
    19         for (int i = 0; i <= CheckBoxList1.Items.Count - 1; i++)  
    20         {  
    21             if (arrtmp[i] != CheckBoxList1.Items[i].Selected)  
    22             {  
    23                 arrtmp[i] = CheckBoxList1.Items[i].Selected;  
    24                 clicknum = i;  
    25                 break;  
    26             }  
    27         }  
    28         if (clicknum >= 0)  
    29         {  
    30             Response.Write(CheckBoxList1.Items[clicknum].ToString() + "   caused the event!");  
    31         }  
    32     }  
    33  

    Hope this will help you.


    Kind regards,
    Guo Surfer

    Tuesday, October 28, 2008 10:32 AM