0

i have a textarea which i want to disable when I check a checkbox. I made a working demo in plain HTML and JavaScript , but When I am migrating to Asp.net, neither the function is being executed nor I can keep the OnSelect event in my check box.

<asp:CheckBox ID="ChkOthers" runat="server" Text="Others(Please Explain)" Width="205px" AutoPostBack="True" OnCheckedChanged="ChkOthers_CheckedChanged" />
<asp:TextBox ID="TxtOtherReason" runat="server" MaxLength="1024" TextMode="MultiLine" />

i want to create client side validation when check box is checked textbox is enable else disable.

abatishchev
  • 98,240
  • 88
  • 296
  • 433

4 Answers4

3

I guess you problem is in the id of the controls so here is my solution for your problem

//Remove the AutoPostBack property and the OnCheckedChanged="ChkOthers_CheckedChanged"
     <asp:CheckBox ID="ChkOthers" runat="server" Text="Others(Please Explain)" Width="205px"  OnClick="javascript:YourFunction()" />
        <asp:TextBox ID="TxtOtherReason" runat="server" MaxLength="1024" TextMode="MultiLine" />

and put the following javascript to handle the enable and diable of the textbox

<script>
    function YourFunction() {
        document.getElementById('<%= TxtOtherReason.ClientID %>').disabled = document.getElementById('<%= ChkOthers.ClientID %>').checked == false ? true : false;
    }
</script>
Samir Adel
  • 2,691
  • 2
  • 15
  • 16
1

It has been a while for me since I used ASP.NET but from memory there is something like an "OnClientClick" event and a validation property in asp.net properties for a control that you can use to call client side JavaScript code..

Sorry for the vague answer - perhaps someone can expand on it.

filip-fku
  • 3,265
  • 1
  • 21
  • 19
0

You are using a server side handling of the event. Use a client side handling with "OnClientClick". You can do something like:

<asp:CheckBox ID="ChkOthers" runat="server" 
     Text="Others(Please Explain)" 
     Width="205px" 
     AutoPostBack="True" 
     OnClientClick="alert(this.checked);" 
     OnCheckedChanged="ChkOthers_CheckedChanged" />

It seems this also does not work. I am yet to prove.

Also see a similar post on StackOverflow: OnClick vs OnClientClick for an asp:CheckBox? and there is also an issue in MS site: http://connect.microsoft.com/VisualStudio/feedback/details/103398/checkbox-onclientclick

Community
  • 1
  • 1
Kangkan
  • 15,267
  • 10
  • 70
  • 113
0

you can put the code in cs files .after it loaded you can make the onclientclick method to an exist javascript function . and in the javascript function you can get the text box use the name or the simple id which you can get from the source file the server gives to the browser!

xingyue
  • 122
  • 1
  • 9