Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6752
  • Last Modified:

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

Hi,

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();">
</asp:CheckBox>

OR

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?

Thanks!

CatWithAHat

0
CatWithAHat
Asked:
CatWithAHat
  • 2
1 Solution
 
YZlatCommented:
try onclick event:

chkb1.Attibutes.Add("onclick", "javascript:MyFunction();");
0
 
archrajanCommented:
just do this
<input type = "checkbox" name = "check1" id = "check1" onchange = "myfunction();"></input>
0
 
archrajanCommented:
try onclick.....
0
 
CatWithAHatAuthor Commented:
Thanks YZlat!  Works perfectly.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now