在asp.net中使用复选框列表? [英] using checkbox list in asp.net?
问题描述
亲爱的所有人,
设计:
Dear All,
Design:
<table class="style1">
<tr>
<td align="right">
Name :</td>
<td>
<asp:TextBox ID="TextBox1" runat="server">
</td>
</tr>
<tr>
<td align="right">
Address :</td>
<td>
<asp:TextBox ID="TextBox2" runat="server">
</td>
</tr>
<tr>
<td align="right">
Mobile :</td>
<td>
<asp:TextBox ID="TextBox3" runat="server">
</td>
</tr>
<tr>
<td align="right">
Product Items :</td>
<td>
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem>LapTop
<asp:ListItem>Mobile
<asp:ListItem>Bike
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
<asp:Label ID="Label1" runat="server" Text="Label">
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
</td>
</tr>
</table>
代码:
Code:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
con.Open();
string str = "insert into CheckTable values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + CheckBoxList1.SelectedItem.Value + "')";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
Label1.Text = "data inserted sucessfully";
}
这里填写以下名称:kkkk,地址:hhhhhh,mobile:1234567890,产品项目:laptop,mobile,bike
在数据库表中填充商店之后但不存储如下:
列:(名称,地址, mobile,productItems)= kkkk,hhhhhh,1234567890,笔记本电脑,手机,自行车
但是将数据存储在表格中:kkkk,hhhhhh,1234567890,laptop
这里不存储复选框列表所有数据,我想要所有请回复我任何示例和上面的代码任何错误请回复我
Here once fill these like name: kkkk, address: hhhhhh, mobile: 1234567890, product items: laptop, mobile, bike
here after fill store in database table but not storing like:
columns:(name,address,mobile,productItems)= kkkk,hhhhhh,1234567890,laptop,mobile,bike
but storing data in table like:kkkk,hhhhhh,1234567890,laptop
here not store checkbox list all data and i want all please reply me any examples and above code any mistakes please reply me
推荐答案
更新您的代码如下:
Update your code as below:
protected void Button1_Click(object sender, EventArgs e)
{
string selectedValues = "";
foreach (var item in CheckBoxList1.Items)
{
selectedValues = selectedValues + "," + item.ToString();
}
selectedValues = selectedValues.Substring(1, selectedValues.Length - 1);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
con.Open();
string str = "insert into CheckTable values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + selectedValues + "')";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
Label1.Text = "data inserted sucessfully";
}
您必须遍历所有选定的项目。并存储在不同的变量中,然后尝试插入。
You have to loop through all the selected Items. and store in different variables then try to insert.
这篇关于在asp.net中使用复选框列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!