Search code examples
javascriptasp.netajaxcontroltoolkit

.Net Listbox will not compile with javascript event handler?


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!


Solution

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

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