开发者

.Net Listbox will not compile with javascript event handler?

开发者 https://www.devze.com 2023-02-13 05:13 出处:网络
This should be the simplest thing ever but it will not work. I have a simple asp.net Listbox and for the event OnSelectedIndexChanged I want to launch a javascript function. This works for when i set

This should be the simplest thing ever but it will not work. I have a simple asp.net Listbox and for the event OnSelectedIndexChanged I want to launch a javascript function. This works for when i set links to launch the same function but not when set for this particular control. The line of code is as follows:

<tr><td>
    <asp:ListBox ID="ListBox1" runat="server" Width="250" 
    Height="600" OnSelectedIndexChanged="javascript:开发者_开发问答selectedIndexChanged()">
    </asp:ListBox>
</td></tr>

Here are the compilation errors i am getting:

c:\..\ManufInfo.aspx(171,84): error CS1026: ) expected

c:\..\ManufInfo.aspx(171,84): error CS1002: ; expected

c:\..\ManufInfo.aspx(171,84): error CS1525: Invalid expression term ':'

c:\..\ManufInfo.aspx(171,84): error CS1026: ) expected

c:\..\ManufInfo.aspx(171,84): error CS1002: ; expected

c:\..\ManufInfo.aspx(171,84): error CS1525: Invalid expression term ':'

c:\..\ManufInfo.aspx(171,85): error CS1002: ; expected

c:\..\ManufInfo.aspx(171,85): error CS1002: ; expected

c:\..\ManufInfo.aspx(171,107): error CS1002: ; expected

c:\..\ManufInfo.aspx(171,107): error CS1525: Invalid expression term ')'

c:\..\ManufInfo.aspx(171,107): error CS1002: ; expected

c:\..\ManufInfo.aspx(171,107): error CS1525: Invalid expression term ')'

What the heck is going? ;) Probably a n00b mistake but I thought I was picking up jscript enough to understand that should work...

Thanks to anyone who can point me in the right direction!


OnSelectedIndexChanged is not meant for javascript handlers. Try this on page_load

ListBox1.Attributes.Add("onclick", "selectedIndexChanged()");


The value that goes in the OnSelectedIndexChanged attribute must be the name of a method in the code-behind (or code-beside) that matches the signature of the EventHandler delegate, which is executed on post back.

SOE has provided the correct way to add a javascript onclick event to your control.


OnSelectedIndexChanged is server side. Will not trigger a javascript, it will refer to a codebehind method.


This attribute is designed for ASP.Net events, not for clientside event handling.

You can attach an eventhandler after the control is rendered like this:

document.getElementById('<%=ListBox1.ClientID %>').onChange = function (){
    // your selectedIndexChanged logic goes here
};
0

精彩评论

暂无评论...
验证码 换一张
取 消