Brendan Russo
Brendan Russo

Reputation: 176

asp:ListBox OnSelectedIndexChanged call javascript function not server side code

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.

Upvotes: 1

Views: 8057

Answers (1)

Dnyanesh
Dnyanesh

Reputation: 2343

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);
    }

Upvotes: 3

Related Questions