如何将CheckBox列表值提交到数据库中 [英] How do I submit CheckBox list value into the database

查看:63
本文介绍了如何将CheckBox列表值提交到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,它允许我从checklistBox控件向sql 2008数据库提交多个选中的值。



i希望用户填写textBox,选择值从下拉菜单和复选框....按下按钮后...它应该提交到数据库中。



提前谢谢



见下面的html结构:



 < ;   div  >  

< class = style1 >
< tr >
& lt; td class = style3 >
PolicyNumber < / td >
< td class = style2 >
< asp:TextBox ID = TextBox1 runat = server > < / asp:TextBox >
< / td >
< / tr >
< tr >
< td class = style3 >
Business < / td >
< td < span class =code-attribute> class = style2 >
< asp:DropDownList ID = DropDownList1 runat = server < span class =code-attribute> AutoPostBack = True

onselectedindexchanged = DropDownList1_SelectedIndexChanged >
< asp:ListItem > 选择项目< ; / asp:ListItem >
< asp:ListItem > 私人< / asp:ListItem >
< asp:ListItem > 商业< / asp:ListItem >
< / asp:DropDownList >
< / td >
< / tr >
< ; tr >
< td class = style3 >
输入< / td >
< td class = style2 >
< asp:DropDownList ID = DropDownList2 runat = server 可见 = 错误 >
< / asp:DropDownList < span class =code-keyword>>
< / td >
< / tr >
< tr >
< td class = style3 bgcolor = 白色 >
产品< / td >
<温泉n class =code-keyword>< td class = style2 >
& nbsp; < / td >
< / tr >
< < span class =code-leadattribute> tr >
< td class = style3 bgcolor = 白色 >
& nbsp; < / td >
< td class = style2 >
< asp:CheckBoxList ID = CheckBoxList1 runat = server

DataSourceID = SqlDataSource1 DataTextField = 产品 DataValueField = 产品

< span class =code-attribute> RepeatColumns
= 2 RepeatDirection = 水平 宽度 = 450px >
< / asp:CheckBoxList >
< asp:SqlDataSource ID = SqlDataSource1 runat = server

ConnectionString = <% $ ConnectionStrings:MotorConnectionString %>

SelectCommand = SELECT * FROM [Product] > < / asp:SqlDataSource >
< / td >
< / tr >
< tr >
< ; td class < span class =code-keyword> = style3 >
& nbsp; < / td >
< td class = style2 >
< < span class =code-leadattribute> asp:按钮 ID = Button1 runat = server 文字 = 提交 字体粗体 = < span class =code-keyword> True

字体大小 = 高度 = 45px 宽度 = 150px / >
< / td >
< / tr >
< / table >

< / div >

回复快速回复报告垃圾邮件编辑删除

解决方案

ConnectionStrings: MotorConnectionString %>

SelectCommand = SELECT * FROM [Product] < span class =code-keyword>> < / asp:SqlDataSource < span class =code-keyword>>
< / td >
< / tr >
< tr >
< td class = style3 >
& nbsp; < / td >
< td class = style2 >
< asp:按钮 ID = Button1 runat = server 文字 = 提交 字体粗体 =

字体大小 = 高度 = 45px Width = 150px / >
< / td >
< / tr >
< / table >

< / div >

回复快速回复报告垃圾邮件编辑删除


这是我的后面代码



< pre lang =cs> strCon = ConfigurationManager.ConnectionStrings [ MotorConnectionString]。ConnectionString;
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected == true
{
使用(SqlConnection sqlcon = new SqlConnection(strCon))
{
using (SqlCommand sqlcmd = new SqlCommand( INSERT INTO testProduct VALUES(@ PolNo,@ Business,@ Type,@ Product1,@ Product1。@ Product3)))
{
sqlcmd.CommandType = CommandType。文本;
sqlcmd.Parameters.AddWithValue( @ PolNo,TextBox1.Text);
sqlcmd.Parameters.AddWithValue( @ Business,DropDownList1.SelectedValue);
sqlcmd.Parameters.AddWithValue( @ Type,DropDownList2.SelectedValue);
sqlcmd.Parameters.AddWithValue( @ Product1,CheckBoxList1.SelectedValue);
sqlcmd.Parameters.AddWithValue( @ Product2,CheckBoxList1.SelectedValue);
sqlcmd.Parameters.AddWithValue( @ Product3,CheckBoxList1.SelectedValue);
sqlcmd.Connection = sqlcon;

sqlcon.Open();
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
}
}
}


i am working on a project that will allow me submit multiple selected value from checklistBox control to sql 2008 database.

i want users to fill the textBox,pick values from the dropdown and check boxes.... after hitting the button... it shoud submit into the database.

Thanks in advance

see below the html structure:

<div>

  <table class="style1">
  <tr>
  <td class="style3">
  PolicyNumber</td>
  <td class="style2">
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  </td>
  </tr>
  <tr>
  <td class="style3">
  Business</td>
  <td class="style2">
  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

  onselectedindexchanged="DropDownList1_SelectedIndexChanged">
  <asp:ListItem>Select Item</asp:ListItem>
  <asp:ListItem>Private</asp:ListItem>
  <asp:ListItem>Commercial</asp:ListItem>
  </asp:DropDownList>
  </td>
  </tr>
  <tr>
  <td class="style3">
  Type</td>
  <td class="style2">
  <asp:DropDownList ID="DropDownList2" runat="server" Visible="False">
  </asp:DropDownList>
  </td>
  </tr>
  <tr>
  <td class="style3" bgcolor="White">
  Product</td>
  <td class="style2">
  &nbsp;</td>
  </tr>
  <tr>
  <td class="style3" bgcolor="White">
  &nbsp;</td>
  <td class="style2">
  <asp:CheckBoxList ID="CheckBoxList1" runat="server"

  DataSourceID="SqlDataSource1" DataTextField="Product" DataValueField="Product"

  RepeatColumns="2" RepeatDirection="Horizontal" Width="450px">
  </asp:CheckBoxList>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server"

  ConnectionString="<%$ ConnectionStrings:MotorConnectionString %>"

  SelectCommand="SELECT * FROM [Product]"></asp:SqlDataSource>
  </td>
  </tr>
  <tr>
  <td class="style3">
  &nbsp;</td>
  <td class="style2">
  <asp:Button ID="Button1" runat="server" Text="Submit" Font-Bold="True"

  Font-Size="Large" Height="45px" Width="150px" />
  </td>
  </tr>
  </table>

  </div>

Reply Quick Reply Report a Spam Edit Delete

解决方案

ConnectionStrings:MotorConnectionString %>" SelectCommand="SELECT * FROM [Product]"></asp:SqlDataSource> </td> </tr> <tr> <td class="style3"> &nbsp;</td> <td class="style2"> <asp:Button ID="Button1" runat="server" Text="Submit" Font-Bold="True" Font-Size="Large" Height="45px" Width="150px" /> </td> </tr> </table> </div> Reply Quick Reply Report a Spam Edit Delete


Here is my behind code

strCon = ConfigurationManager.ConnectionStrings["MotorConnectionString"].ConnectionString;
       foreach (ListItem li in CheckBoxList1.Items)
       {
           if (li.Selected == true)
           {
               using (SqlConnection sqlcon = new SqlConnection(strCon))
               {
                   using (SqlCommand sqlcmd = new SqlCommand("INSERT INTO testProduct VALUES (@PolNo,@Business,@Type,@Product1,@Product1.@Product3)"))
                   {
                       sqlcmd.CommandType = CommandType.Text;
                       sqlcmd.Parameters.AddWithValue("@PolNo", TextBox1.Text);
                       sqlcmd.Parameters.AddWithValue("@Business", DropDownList1.SelectedValue);
                       sqlcmd.Parameters.AddWithValue("@Type", DropDownList2.SelectedValue);
                       sqlcmd.Parameters.AddWithValue("@Product1", CheckBoxList1.SelectedValue);
                       sqlcmd.Parameters.AddWithValue("@Product2", CheckBoxList1.SelectedValue);
                       sqlcmd.Parameters.AddWithValue("@Product3", CheckBoxList1.SelectedValue);
                       sqlcmd.Connection = sqlcon;

                       sqlcon.Open();
                       sqlcmd.ExecuteNonQuery();
                       sqlcon.Close();
                   }
               }
           }


这篇关于如何将CheckBox列表值提交到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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