atrljoe
atrljoe

Reputation: 8151

C# CheckBox Question

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.

Upvotes: 1

Views: 1094

Answers (3)

apros
apros

Reputation: 2878

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

Upvotes: 0

Joe
Joe

Reputation: 57169

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.

Upvotes: 1

Oded
Oded

Reputation: 498952

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

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

Upvotes: 2

Related Questions