从CheckBoxList DataSource获取selectedvalue

时间:2015-03-13 23:01:06

标签: c# asp.net

我有控制权:

 <asp:CheckBoxList ID="CheckBoxListdepts" AutoPostBack="true" BorderWidth="1PX" BorderColor="#3f3a71" Width="100%" runat="server" 
                        DataSourceID="SqlDataSourcedepts"
                         DataTextField="dept_name" 
                        DataValueField="dept_id"
                         OnSelectedIndexChanged="CheckBoxListdepts_SelectedIndexChanged">

我有一个包含5个CheckBoxList的数组,这是我的代码

protected void CheckBoxListdepts_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckBoxList[] mylists = { CheckBoxList1, CheckBoxList2, CheckBoxList3, CheckBoxList4, CheckBoxList5 };

            for (int i = 0; i < 5; i++)
            {
                mylists[i].DataSource = (from ListItem rr in CheckBoxListdepts.Items
                                         where rr.Selected == true
                                         select rr);    


 mylists[i].DataBind();


            }           
        }

问题是当我尝试从checkbox1list1获取所选值时,我得到了selectedText 像这样:

  protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label7.Text = CheckBoxList1.SelectedItem.Value.ToString();
        }

我得到了SelectedText 为什么“rr”失去了它的价值财产?

1 个答案:

答案 0 :(得分:1)

我不知道你的5个CheckBoxLists是什么样的,但我认为你必须设置他们的DataValueField和DataTextField属性才能得到你想要的东西:

<asp:CheckBoxList1 runat="server" DataValueField="Value" DataTextField="Text" />