0

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?

4

1 回答 1

0

如果您的项目是 Asp.Net,并且您在页面加载事件中填充了 listBox 控件,则只需放置此控件:

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

因为,当您发布页面时,页面加载事件会触发并再次填充您的列表框。所以你的选择是明确的。

于 2013-11-04T12:35:06.867 回答