Adding attribute OnChange to a CheckBox webcontrol - ASP.NET c#

Posted on 2004-11-30
Last Modified: 2009-11-09

I am trying to add an attribute OnChange to a CheckBox control, I have tried doing this both in HTML and in the code behind like so:

<asp:CheckBox id="chkb1" runat="server" OnChange="javascript:MyFunction();">


chkb1.Attibutes.Add("OnChange", "javascript:MyFunction();");

As you can see all I'm trying to do is fire a javascript function.  The page renders fine but on testing the checkbox the OnChange event does not get fired.  On viewing the source code I found that the OnChange event was rendered in the <span> tag and not the <input> tag.

I know a CheckBox does not have the OnChange event associate with it but has the OnCheckedChanged event, so i tried this too but got an error.  Can I use the OnCheckedChange function to fire off a javascript function?

Whether i use the OnChange or OnCheckedChanged event how can i get it to render in the <input> tag of the checkbox instead of the <span> tag?



Question by:CatWithAHat
    LVL 35

    Accepted Solution

    try onclick event:

    chkb1.Attibutes.Add("onclick", "javascript:MyFunction();");
    LVL 25

    Expert Comment

    just do this
    <input type = "checkbox" name = "check1" id = "check1" onchange = "myfunction();"></input>
    LVL 25

    Expert Comment

    try onclick.....

    Author Comment

    Thanks YZlat!  Works perfectly.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now