Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Javascript set button enable on checkbox true
Message
From
15/10/2010 10:00:56
 
 
To
15/10/2010 09:36:34
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 4.0
Application:
Web
Miscellaneous
Thread ID:
01485567
Message ID:
01485583
Views:
37
>>>Hi all,
>>>
>>>I have what I thought would be a simple task. I have a user control on an asp.net page showing an agreement form. All I want to do is enable a continue button after a checkbox is checked to indicate agreement. I wanted to avoid a postback so am trying to set the button to enabled when the checkbox is checked but I cannot get this to work. I have tried several approaches found on web including the below. (I changed the angle brackets to parens so they would show)
>>>
>>>Anybody with javascript knowledge have an answer that might help me? Thanks a bunch
>>>Tim
>>>
>>>
>>>This is just one approach I tried.
>>>
>>>     (mm:mmCheckBox ID="chkAgree" runat="server" Checked="false" Text="I Agree to the Terms" /)
>>>             
>>>     (mm:mmButton ID="btnAgreeContinue" runat="server" Text="Continue" 
>>>                Enabled="False" /)
>>>     
>>>function SetAgreeAndContinue(checkbox, buttonid) {
>>>        var button = document.getElementById(buttonid);
>>>
>>>        if (checkbox.checked)
>>>            button.enabled = true;
>>>        else
>>>            button.enabled = false;
>>>    }
>>>
>>>In the code behind
>>>protected override void OnInit(EventArgs e)
>>>{
>>>     chkAgree.Attributes.Add("onClick", string.Format("javascript:SetAgreeAndContinue(this,'{0}');", btnAgreeContinue.ClientID));
>>>     base.OnInit(e);
>>>}
>>>
>>
>>Haven't tried your code but something based on this should work (assuming the mm controls are derived from the asp ones):
<head runat="server">
>>    <title></title>
>></head>
>>	<script language="javascript" type="text/javascript">
>>	    function SetAgreeAndContinue(checkbox) {
>>	        var button = document.getElementById("btnAgreeContinue");
>>	        button.disabled = !checkbox.checked;
>>	    }
>>	</script>
>>
>><body>
>>    <form id="form1" runat="server">
>>    <div>
>>        <asp:CheckBox ID="chkAgree" runat="server" onclick="SetAgreeAndContinue(this)" />
>>        <asp:Button ID="btnAgreeContinue" runat="server" Text="OK" disabled="true" />
>>    </div>
>>    </form>
>></body>
>
>Thanks Viv,
>
>How would this be different since I am using a web user control. I cannot put the script in the head section (there isn't one). I suppose I could add a content place holder in the head of the master page, but not sure I could add to that within the web user control. Maybe I need to inject it via code.

Try this in the Page_Load:
Page.RegisterStartupScript("S",
    @"<script language='javascript' type='text/javascript'>
	    function SetAgreeAndContinue(checkbox) {
	        var button = document.getElementById('btnAgreeContinue');
	        button.disabled = !checkbox.checked;
	    }
	</script>");
I think Page.RegisterStartupScript has been made obsolete so you might want to check out the ClientScript class version.....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform