1

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.

Mike
  • 81
  • 6

1 Answers1

2

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

Community
  • 1
  • 1
L_7337
  • 2,650
  • 28
  • 42
  • 1
    That works, but if I have other dropdownlists or selects on the page that are being populated on `page_load` their selection is reset when the `OnSelectedIndexChanged` fires. Appologies for not explaining my entire situation in the first post – Mike Jul 23 '14 at 13:10
  • Really, it should be something like `controlName1_OnSelectedIndexChanged`. Each control would need its own event for when the selected index changes. You should also be checking if its a Postback before re-populating on `page_load` – L_7337 Jul 23 '14 at 13:13
  • I've got it sorted now, thank you so much for pointing me in the right direction, you're a star. – Mike Jul 23 '14 at 13:42