k80sg
k80sg

Reputation: 2473

Accessing HTML Checkbox in code behind

I have a HTML checkbox which will perform a simple validation on another textbox, so the textbox will only be enabled if the checkbox is checked, but I also want to access this checkbox control in code behind to check and uncheck it. I don't think I can use the runat="server" because on the onClick event which will cause ('<%=uitxtVouTypeRedeemValue.ClientID%>') to be output as plain text. Please advice. Thanks.

<input type="checkbox" 
 onclick="document.getElementById('<%=uitxtVouTypeRedeemValue.ClientID%>').disabled =(this.checked)?0:1" id="uichkVouTypeRedeemable" />

Upvotes: 0

Views: 3403

Answers (2)

competent_tech
competent_tech

Reputation: 44921

In order to access it from code-behind, you must have runat="server" specified.

To resolve your issue with the onclick event, you can add the following code in your code-behind during the control or page's Load event:

uichkVouTypeRedeemable.Attributes.Add("onclick", "document.getElementById('" & uitxtVouTypeRedeemValue.ClientID & "').disabled =(this.checked)?0:1;")

Upvotes: 1

Pawan Mishra
Pawan Mishra

Reputation: 7268

You can access the Form data in code behind. See the following Stack Overflow thread :

How to access html form input from asp.net code behind

Essentially, the checkbox checked status will be posted, once the form is submitted. You can then access this information, in the code-behind.

Upvotes: 0

Related Questions