0

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" />

2 Answers 2

1

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;")
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! A minor correction for others looking at this: ('" & uitxtVouTypeRedeemValue.ClientID & "') should have ' '
0

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.

1 Comment

Hi, besides getting the value, how can I check and uncheck it from code behind?

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.