1

I have HTML Generated Checkboxes on a page. How can I check to see if they are 'Checked' with c#? I am looking to use an if statement

if (checkbox.checked = true)
   {
        // EXECUTE CODE HERE
   }

I don't know how to call the element since it's HTML.

For my HTML I use another aspx to generate the HTML

FileListLabel.Text += ("<input type='checkbox' name='option" + counter +
                       "' value='" + SPEncode.HtmlEncode(oListItem["ID"].ToString()) +
                       "'>" + SPEncode.HtmlEncode(oListItem["LinkFilename"].ToString()) + "<BR>");

Is there a way to make that runat server? Or Should I use the Request.Form?

Thank you.

2
  • can you show some of your HTML? Are you using ASP.Net? ASP.NET MVC? Commented Feb 3, 2011 at 13:59
  • Can't provide solid answer here, but I guess you should be able to get the html using the WebBrowser object, followed by using something similar to getElementById(myCheckboxId).attribute("checked"); Note that I havn't tested this, but see if it helps you. Commented Feb 3, 2011 at 14:03

3 Answers 3

2

An HTML checkbox will only be submitted if it is checked.

So, if it exists in the postback, it was checked.

Sign up to request clarification or add additional context in comments.

Comments

1

With C# you either need to have the checkboxes declared as runat="server" to access by name, or check the Request.Form for the value.

Comments

0

In my opinion you should do this with javascript only in a way like <input onclick="__doPostBack('__Page', 'yourCheckboxNumberNIsChecked');" /> // where yourCheckboxNumberNIsChecked is flag which you will set when you define that checkbox has property checked set in "true". Then in code-behind you can define this event in a such way:

If Page.IsPostBack Then
   Dim eventArg As String = Request("__EVENTARGUMENT")
   If eventArg = "yourCheckboxNumberNIsChecked" Then
       Response.Write("You check it !")
   End If
End If

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.