如何从C#中的复选框中获取选定的值

时间:2017-12-02 05:49:48

标签: c# asp.net checkbox insert-update

我在insert.aspx中有以下代码行。

Source Code:
 <asp:CheckBox ID="chkIncludeSignature" runat="server" Text="Include Signature" />

Code Behind:
 command1.Parameters.AddWithValue("?IncludeSignature", chkIncludeSignature.Checked ? 1 : 0);

这是update.aspx的代码:

Source Code:
<asp:CheckBox ID="chkIncludeSignature" runat="server" Text="Include Signature" />

Code Behind:
 chkIncludeSignature.Checked=dtInvoice.Rows[0]["IncludeSignature"].ToString();

我在db表中插入了复选框值。我希望在插入表单中插入的更新表单中的选定值。我收到错误为“无法将类型字符串转换为bool”。

3 个答案:

答案 0 :(得分:1)

chkIncludeSignature.Checked=dtInvoice.Rows[0]["IncludeSignature"] == 1 ? true : false;

答案 1 :(得分:0)

删除最后一行代码末尾的.ToString()。如果数据库中IncludeSignaturebit数据类型,则dtInvoice.Rows[0]["IncludeSignature"]必须返回bool值。您也可以使用bool.TryParse(dtInvoice.Rows[0]["IncludeSignature"].ToString(), out boolVariable);

答案 2 :(得分:0)

我刚刚更新了这一行...并且工作正常......:)

chkIncludeSignature.Checked= Convert.ToBoolean(dtInvoice.Rows[0]["IncludeSignature"]);