Question

at first here is my code:

foreach (ListItem l in l_roles.Items)
                {
                    if (l.Selected)
                    {
                        SqlCommand find_r = new SqlCommand("SELECT id FROM roles WHERE rolename=@rolename", conn);
                        find_r.Parameters.AddWithValue("@rolename", l.Text);
                        f = find_r.ExecuteScalar().ToString();

                        SqlCommand fill_g_r = new SqlCommand("INSERT INTO groups_roles (group_id, role_id) VALUE (@group_id, role_id)", conn);
                        fill_g_r.Parameters.AddWithValue("@group_id", l_gr_id.Text);
                        fill_g_r.Parameters.AddWithValue("@role_id", f);
                        fill_g_r.ExecuteNonQuery();
                    }


                }

When i'm testing this code in the debug-mode, choose an item in the listbox and hit the button, nothing happened.

If I set a breakpoint at the end of the "foreach", I can see that:

if (l.Selected)

is false. the

l.Text

is the right listitem i'd choosed before. I tried also other possibilities like:

if(!l.selected) or if(l.selected == true)

I hope anyone has an idea?

Was it helpful?

Solution

If your project is Asp.Net and you fill your listBox control in page load event, just put this control:

if(!IspostBack)
 this.LoadListBox(); //your fill method

Because, when you post the page, page load event trig and fill your listBox again. So your selection is clearing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top