1

I am needing to call a javascript function when my asp:ListBox has a selection change rather then running some server side code. What I have at the moment is.

<asp:ListBox ID="lbCustomerFolders" runat="server" Width="100%" Height="98%" AutoPostBack="true" OnSelectedIndexChanged="lbCustromerFolders_SelectedIndexChanged"/>

Where the OnSelectedIndexChanged I require that function or something similar to call a javascript function.

1 Answer 1

3

Add on change event in your control lie I have added in following example. Notice that there are onchange as well as OnSelectedIndexChanged event there on the ListBox so on the selection change event both JavaScript and server side event is going to get called.

In your case change would be data which you are providing.

 <asp:ListBox ID="lbCustomerFolders" runat="server" Width="9%" Height="98%"  onchange="YourChangeEventJS(this)" AutoPostBack="true" OnSelectedIndexChanged="lbCustomerFolders_SelectedIndexChanged">
             <asp:ListItem Text="Red" Value="#FF0000" Selected="True" />
             <asp:ListItem Text="Blue" Value="#0000FF" />
             <asp:ListItem Text="Green" Value="#008000" />
         </asp:ListBox>

Following is script should be there on your page

 <script type="text/javascript">
    function YourChangeEventJS(ddl) {
        alert(ddl.selectedIndex);
    }

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

3 Comments

This doesn't work for me. I've added the onchange to ListBox and the javascript doesn't fire. Did I add it in the wrong place my listbox now looks like: <asp:ListBox ID="lbCustomerFolders" runat="server" Width="100%" Height="98%" onchange="YourChangeEventJS(this);"/>
@BrendanRussn I have edited answer with changes. Hope this will help you.
Well it wasn't working at first but after changing something this now works with my full listbox. Thanks

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.