简体   繁体   English

使用C#,MSSQL将值从Asp.net中的数据库绑定到复选框

[英]Bind value to checkbox from database in Asp.net using C#, MSSQL

Am working on a registration page, Where here user has checkbox to select the location and services, Am saving the data in Database as 1 if checked , 0 if form is saved unchecked. 在注册页面上工作,此处用户具有用于选择位置和服务的复选框,如果选中,则将数据保存在数据库中为1,如果不选中表单,则将数据保存为0。

Now in the Edit page for the user Like profile page, am binding the data which is present in database. 现在,在用户喜欢个人资料页面的编辑页面中,对数据库中存在的数据进行绑定。

I don't know how to bind the checkbox to bind if the user already checked and in database it is saved as '1" 我不知道如何绑定复选框,如果用户已经检查过并在数据库中将其另存为“ 1”

Am using C# and Asp.net. 我正在使用C#和Asp.net。 Kindly Help 请帮助

You can convert you int value 1 or 0 and then assign to your check-box checked property. 您可以将int值转换为10 ,然后将其分配给您的复选框checked属性。 If they are string type then convert them to int and then to Boolean 如果它们是字符串类型,则将它们转换为int然后转换为Boolean

 CheckBox1.Checked = Convert.ToBoolean(0);  // False - Not checked.
CheckBox1.Checked = Convert.ToBoolean(1);   // True - checked.

You can use this method Convert.ToBoolean(value) to convert string to boolean 您可以使用此方法Convert.ToBoolean(value)将字符串转换为布尔值

CheckBox1.Checked = Convert.ToBoolean("false");  // False - Not checked.
CheckBox1.Checked = Convert.ToBoolean("true");   // True - checked.

You will get either 0 or 1 from database right?? 您将从数据库中获得0或1吗?? SO, store it in integer variable a; 因此,将其存储在整数变量a中;

{
if (a==0)
chkbox1.checked = false
else
chkbox1.checked = true;
}
<asp:checkbox id="Cluster" runat="server" checked='<%# Eval("Cluster") == DBNull.Value ? false : Eval("Cluster") %>' />

您可以使用以下代码从数据库中获取值并将其以Web形式表示:

CheckBox1.Checked = rdr.GetBoolean(3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM