如何获取CheckboxList控件的值 [英] how to get Value of CheckboxList control

查看:70
本文介绍了如何获取CheckboxList控件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,
我有一个CheckboxList控件,如下所示:

Hi friends,
i have a one CheckboxList control like below:

<asp:CheckBoxList ID="cblGA">
<asp:ListItem Text="African American/African Heritage" Value="0" >
<asp:ListItem Text="African American/African Heritage" Value="0" >
                 <asp:ListItem Text="Asian-Central/South Asian Heritage" Value="1">
                 <asp:ListItem Text="Asian-Japenese Heritage" Value="2">
                 <asp:ListItem Text="Native Hawaiian or Other Pacific Islander" Value="3">
                 <asp:ListItem Text="White-White/Caucasian/European Heritage" Value="4">
               
                <asp:ListItem Text="American Indian or Alaskan Native" Value="5">
                 <asp:ListItem Text="Asian-East Asian Heritage" Value="6">
                 <asp:ListItem Text="Asian-South East Asian Heritage" Value="7">
                 <asp:ListItem Text="White-Arabic/North African Heritage" Value="8">
                  <asp:ListItem Text="Other" Value="9" />




我想从复选框列表中获取值,即使未选中某些值也将其保存在数据库中.
怎么做?

请回复




i want to get the values from the checkbox list even if some are not selected and save it in Database.
how to do it?

pl reply

推荐答案

<asp:CheckBoxList  runat ="server"    ID="cblGA">
<asp:ListItem Value="A" >AA</asp:ListItem>
<asp:ListItem Value="B">BB</asp:ListItem>
<asp:ListItem Value="C">CC</asp:ListItem>
</asp:CheckBoxList>

and write on code behind

 
          <pre lang="c#">  string chkboxlistValue = "";
            foreach (ListItem val in cblGA.Items)
            {
                if (val.Selected)
                {
                    chkboxlistValue += val.Value + " ";
                }
            }







现在您可以使用变量chkboxlistValue 来保存在数据库中







now you can use variable chkboxlistValue To save in database


string CheckedId = "";
string UncheckedId = "";
foreach (ListItem lst in cblGA.Items)
{
  if (lst.Selected)
  {
     CheckedId += Convert.ToSingle(lst.Value) + ",";
  }
  else
  {
     UncheckedId += lst.Value + ",";
  }
}
if (CheckedId.EndsWith(","))
{
  CheckedId = CheckedId.Remove(CheckedId.Length -1, 1);
}
if (UncheckedId.EndsWith(","))
{
  UncheckedId = UncheckedId.Remove(UncheckedId.Length -1, 1);
}



这里CheckedId包含检查值,而UncheckedId包含未检查值,因此您可以将其与c​​heck和unchecked一起使用.



Here CheckedId Contain Checked Value and UncheckedId contain Unchecked value so u can use it with check and unchecked.


foreach (ListItem li in cblGA.Items)
        {
            if(li.Selected == false)
             {
            // Code to save in database 
              }
        }




我在checkboxlixt中有修复序列,那么可以直接使用索引来获取值,同时保存到数据库中



OR
I you are having fix sequence in checkboxlixt then you can directly use index to get value while saving to database

String strListItem = CheckBoxList1.Items[0].Text;


这篇关于如何获取CheckboxList控件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆