怎样把checkbox选中的值添加到数据库中啊?能给一点代码吗?

怎样把checkbox选中的值添加到数据库中啊?能给一点代码吗?,第1张

简单例子,你在aspx页面上放上这个

你喜欢的食物<br/>

<input type="checkbox" name="chbFood" value="apple" />苹果<br/>

<input type="checkbox" name="chbFood" value="orange" />橘子<br/>

<input type="checkbox" name="chbFood" value="banana" />香蕉<br/>

<input type="checkbox" name="chbFood" value="bread" />面包<br/>

<asp:Button runat="server" Text="提交" ID="btnSubmit"/>

在接收数据的页面的cs文件的Page_Lode里面放上这个

object obj=Request.Form["chbFood"]

if (obj != null)

{

string strFood = (string)obj

Response.Write(strFood)

}

当你做了选择,点击提交,就会在页面上输出你选中的项的value。

如果CheckBox处于选中状态,则为True,否则为False。默认值为False

实例:用CheckBox判断用户是否接受条款协议,如果用户没有选择,则不允许用户注册。单击“注册”按钮,触发btnreg_Click(object sender,EventArgs e)事件

protected void btnreg_Click(object sender, EventArgs e)

{

//判断用户是否接受条款协议

if (c1.Checked)

{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["reg"].ConnectionString)

con.Open()

//定义用户注册的SQL语句

string cmdtex = "insert into users values('" + tname.Text + "','" + tpass.Text + "','" + tmail.Text + "')"

SqlCommand cmd = new SqlCommand(cmdtex, con)

try

{

cmd.ExecuteNonQuery()

con.Close()

Response.Write("<script>alert('注册成功!!')</script>")

}

catch (Exception err)

{

throw new System.Exception(err.Message)

}

}

else

{

Response.Write("<script>alert('请选择接受条款!!')</script>")

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6685196.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存