简体   繁体   中英

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.

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"

Am using C# and Asp.net. Kindly Help

You can convert you int value 1 or 0 and then assign to your check-box checked property. If they are string type then convert them to int and then to 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

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?? SO, store it in integer variable 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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