0

I want to call a javasript function once a checkbox is checked and then enable a button in the function. How can I assign the javascript function to the check box event?
Thanks!

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by apegram
1

I assume this is an ASP.NET implementation using C#?

Here is an example of toggling on/off a button based on a checkbox.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="checkboxdemo.aspx.cs" Inherits="checkboxdemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
        function activateButton(chk)
        {
            baseOfID = chk.id.replace('chkDemo','');
            var button = document.getElementById(baseOfID + 'btnDemo');
            
            if (chk.checked)
                button.disabled = false;
            else 
                button.disabled = true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBox ID="chkDemo" runat="server" Text="Demo" />
        <br /><br />
        <asp:Button ID="btnDemo" runat="server" Text="Demo" Enabled="false" />
    </div>
    </form>
</body>
</html>

And the code-behind to simply register the client-side onclick event.

using System;

public partial class checkboxdemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        chkDemo.Attributes.Add("onclick", "activateButton(this)");
    }
}
0

Oh, and I forgot to mention that I have my master page allied on the .aspx page and therefore cant put the javascript in the <head> tag. Where should I embed the js?

Thanks for your answer it was very useful!

0

Oh, and I forgot to mention that I have my master page allied on the .aspx page and therefore cant put the javascript in the <head> tag. Where should I embed the js?

Thanks for your answer it was very useful!

demo.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="demo.master.cs" Inherits="demo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <asp:ContentPlaceHolder id="headerContent" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="bodyContent" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

checkboxdemo.aspx

<%@ Page Language="C#" MasterPageFile="~/demo.master" AutoEventWireup="true" CodeFile="checkboxdemo.aspx.cs" Inherits="checkboxdemo" %>

<asp:Content ID="Content1" ContentPlaceHolderID="headerContent" Runat="Server">
    <script type="text/javascript" language="javascript">
        function activateButton(chk)
        {
            baseOfID = chk.id.replace('chkDemo','');
            var button = document.getElementById(baseOfID + 'btnDemo');
            
            if (chk.checked)
                button.disabled = false;
            else 
                button.disabled = true;
        }
    </script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="bodyContent" Runat="Server">
    <div>
        <asp:CheckBox ID="chkDemo" runat="server" Text="Demo" />
        <br /><br />
        <asp:Button ID="btnDemo" runat="server" Text="Demo" Enabled="false" />
    </div>
</asp:Content>

Same code-behind as before.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.