Home>

we know,The dropdownlist is a server control.Sometimes, some friends, for the convenience of binding the dropdownlist drop-down box options,But want to implement the client's drop-down event in the dropdownlist,How do you achieve that?

If i want to add client-side drop-down events to the dropdownlist server control,We can force it to add an onchange event, although there is no hint for this method in the control.Adding this event has not yet achieved its purpose,Also set the autopostback property to false to prevent it from posting back events.

Here is the code for everyone:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>dropdownlist add client drop-down event
  <script type="text/javascript">
    function getdropdownlist () {
      var ddl1=document.getelementbyid ("<%= ddl1.clientid%>");
      var text=ddl1.options [ddl1.options.selectedindex] .text;//Get the text value
      var value=ddl1.value;//Get value
      alert ("text:" + ddl1.options [ddl1.options.selectedindex] .text + ", value:" + ddl1.value);
    }
  </script>
</head>
<body>
<form runat="server">
  <asp:dropdownlist runat="server" autopostback="false" onchange="getdropdownlist ();">
    <asp:listitem text="t1" value="v1" selected="true"<&/;asp:listitem>
    <asp:listitem text="t2" value="v2"</asp:listitem>
    <asp:listitem text="t3" value="v3"<&/;asp:listitem>
  </asp:dropdownlist>
</form>
</body>
</html>

in conclusion,That is,To add a client drop-down event to the dropdownlist drop-down box,Must do two steps,One is to add a mandatory onchange event, and the other is to set the autopostback property to falseIt's that simple!

  • Previous Examples of multithreaded programming in Java
  • Next Subview event response and method of obtaining subview in iOS development