1

I have 8 checkboxes.
On the page load event I want to write the code such that
if the value in the database is "Submitted" the checkbox will appear checked.
If the value is "Not Submitted" it will appear unchecked.

3
  • 1
    Have you tried anything? Commented Feb 12, 2013 at 5:36
  • Show us what you've tried already.. Commented Feb 12, 2013 at 5:36
  • check the posted answer or show us your code what you had tried? Commented Feb 12, 2013 at 5:43

3 Answers 3

4

you can have something like this

if(!Page.IsPostBack)
{
     var isChecked = dbvalue.ToLower() == "submitted" ? true : false;
     for (var i = 0; i < 8;i++ )
     {
         CheckBox chk = (CheckBox)Form.FindControl("chk" + i);
         if(chk != null) chk.checked = isChecked;
     }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can check the values like this

if(!Page.IsPostBack)
{
    if(dbvalue=="Submitted")
    {
      chk1.checked=true;
      chk2.checked=true;
      chk3.checked=true;
      chk4.checked=true;
      chk5.checked=true;
      chk6.checked=true;
      chk7.checked=true;
      chk8.checked=true;
    }
    else if(dbvalue=="Not Submitted")
    {
        chk1.checked=false;
      chk2.checked=false;
      chk3.checked=false;
      chk4.checked=false;
      chk5.checked=false;
      chk6.checked=false;
      chk7.checked=false;
      chk8.checked=false;
    }

}

Comments

0

If don't want to do anything in the code-behind, a faster and easy method would be to create a new column of data type bit on the select query and then just bind the checkbox to that. 0 = false and 1 = true. Example would be:

SELECT
CAST((CASE WHEN SubmitColumn = 'Submitted' THEN
    1
ELSE
    0
END) AS BIT) AS chkValue

FROM Table_1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.