1

Environment: .NET 2.0, visual studio 2005

in aspx markup, set a control like:

<asp:CheckBox ID="E287" runat="server"  />

in code behind, hook up event to a javascript func in Page_Load event like:

Me.E287.Attributes.Add("OnCheckedChanged", "javascript:alert(123);")

then test the page in browser, and click on the checkbox, no response. check the source, it is:

<span OnCheckedChanged="javascript:alert();"><input id="E287" type="checkbox" name="E287" /></span>

can't understand.

If change aspx markup like:

<asp:CheckBox ID="E287" runat="server" OnCheckedChanged="javascript:alert(123);" />

will get error

BC30456: 'javascript' is not a member of 'ASP.MyPage_aspx'.

Really confused. How to resolve this problem?

0

3 Answers 3

1

As you see the rendered control the javascript go to the span (label), to add it to the input/check box you need to use the InputAttributes. Also on the client side you need to use the onchange or the onclick to get the results you looking for.

E287.InputAttributes

so it will be

Me.E287.InputAttributes.Add("onchange", "javascript:alert('123');")

Thats because the check box use 2 controls to render, one span and one input, so with your code you add on the text the script and not on check box. There also the LabelAttributes. Here the Attributes are go to the span, because span is parent of input.

 E287.LabelAttributes
Sign up to request clarification or add additional context in comments.

Comments

1

With this code from your code-behind:

Me.E287.Attributes.Add("OnCheckedChanged", "javascript:alert(123);")

OnCheckedChanged is not a client-side (JavaScript) event for a checkbox. You need to change this to onclick. This would explain why this event handler isn't firing.

With your second line of code of wiring up an event:

<asp:CheckBox ID="E287" runat="server" OnCheckedChanged="javascript:alert(123);" />

You're trying to assign a server-side event handler for the checkbox being checked to a method named "javascript:alert(123)" in your code-behind...which doesn't exist.

I would suggest you change your first attempt to wire up the event (using Attributes.Add) to use the onclick event instead:

Me.E287.Attributes.Add("onclick", "javascript:alert(123);")

I hope this helps!

Comments

0

If you're trying to add a javascript event handler to the client side from the server side, try this:

Me.E287.Attributes.Add("onclick", "alert(123);")

On the other hand, if you're trying to add a server side event handler, it should look something like this:

AddHandler E287.CheckedChanged, AddressOf E287_CheckedChanged

and of course you'd have to implement the E287_CheckedChanged event handler method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.