如何存储复选框列表值? [英] How to store checkboxlist value?
本文介绍了如何存储复选框列表值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想存储checkboxlist
的多个值
这是我的复选框列表
I want to store multiple value of checkboxlist
here is my checkbox list
<asp:CheckBoxList ID="chkMaritalStatus" runat="server" CssClass="rb" RepeatDirection="Horizontal">
<asp:ListItem>Unmarried</asp:ListItem>
<asp:ListItem>Widow</asp:ListItem>
<asp:ListItem>Divorced</asp:ListItem>
<asp:ListItem>Awaiting divorce</asp:ListItem>
<asp:ListItem>Any</asp:ListItem>
</asp:CheckBoxList>
并且我想对数据库中的多个项目进行操作?
and i want to multiple items in my database how can i do that?
推荐答案
假设您的数据库包含一个名为MaritalStatus
的列,该列可以接受逗号分隔的值,看看下面的代码是否对您有帮助:
Assuming that your database contains single column calledMaritalStatus
which can accept comma separated values, see if below code helps you:
string status= string.Empty;
foreach (ListItem item in this.CheckBoxList1.Items)
if (item.Selected)
status += item + ",";
SqlCommand cmd = new SqlCommand("Insert into Demo values('" + status+ "')", cn); //Write Your Query to insert into database table
cn.Open();
int iCount = cmd.ExecuteNonQuery();
cn.Close();
Here,chklstSubject is checkbox name and we are store selected items in sub.
String sub="";
for (int i = 0; i < chklstSubject.Items.Count; i++)
{
if (chklstSubject.Items[i].Selected)
{
sub += chklstSubject.Items[i].Value + ",";
}
}
sub = sub.TrimEnd(',');
将sub插入查询,
字符串qr =插入tabl1值(''" + sub +'')";
insert sub into query,
String qr="insert into tabl1 values(''"+sub+"'')";
这篇关于如何存储复选框列表值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文