Mike
Mike

Reputation: 81

OnSelectedIndexChanged Not working

I'm not sure what I'm doing wrong here. I'm try to get an OnSelectedIndexChanged event working, but I'm trying to do it without using the asp form controls.

In the below example the OnServerClick works for the <a> element but neither the OnSelectedIndexChanged nor OnServerClick seem to work for the <select>.

<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html>
<html> 
<head>
<script runat="server">

      Sub HtmlAnchor_Click_1(sender As Object, e As EventArgs)
         Message.InnerHtml = "this doesn't work"
      End Sub

      Sub HtmlAnchor_Click_2(sender As Object, e As EventArgs)
         Message.InnerHtml = "this works"
      End Sub

</script>
</head>
<body>

   <form id="form1" runat="server">

      <select id="AnchorSelect" name="select1" OnSelectedIndexChanged="HtmlAnchor_Click_1" runat="server">
              <option value="volvo">Volvo</option>
              <option value="saab">Saab</option>
              <option value="mercedes">Mercedes</option>
              <option value="audi">Audi</option>
      </select>
      <br /><br />
            <a id="AnchorButton" onserverclick="HtmlAnchor_Click_2" runat="server">Click Here</a>
      <br /><br />

      <span id="Message" runat="server"/>
</form>
</body>
</html>

Any ideas, or solutions would be appreciated. Cheers.

Upvotes: 1

Views: 870

Answers (1)

L_7337
L_7337

Reputation: 2748

select is an HTML input and the OnSelectedIndexChanged would be a Javascript function that gets called.

Use <asp:DropDownList> and set autopostback=true. Then, you would put the OnSelectedIndexChanged in your codebehind to use it.

Check out this example: DropDownList's SelectedIndexChanged event not firing

Upvotes: 2

Related Questions