简体   繁体   中英

javascript onchange event to asp.net dropdownlist

Is it possible to make the dropdown list selection trigger posting back to the same page with whatever was selected added to the url as querystring using javascript? The problem is the list is being loaded dynamically from some sharepoint list. if my site is mysite.com/default.aspx so when a selection is made, it should redirect to mysite.com/default.aspx?key=selection

No server access, No access to codebehind :(

 <asp:DropDownList runat="server" id="DropDownList1" DataSourceID="spdatasource1" DataValueField="CategoryName" AutoPostBack="True" Onchange="window.open( Go to some link)">
                                                 </asp:DropDownList>
var selectedOption = $("#DropDownList1 option:selected").text();

$("#DropDownList1").change(function(e) {
      url: "mysite.com/default.aspx?key=" + selectedOption;
      window.location = url;
});

Untested, also unsure of what event is reloading the page eg (submit or anchor)

Another option (possibly better) from http://tinyurl.com/82ter35

$(document).ready(function() {

   var selectedOption = $("#DropDownList1 option:selected").text();

  $("#DropDownList1").change(function() {
    $.ajax({
      type: "POST",
      url: "mysite.com/default.aspx?key=" + selectedOption,
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Replace the div's content with the page method's return.
        alert("this worked!");
      }
    });
  });
});

I am not sure what you want to do , but I guess jquery is the right answer to your question

anyway this may help :

<script language="javascript" type="text/javascript">
    function xx(e) {
        alert("fired by " + "<%= DropDownList1.UniqueID %>" + "change ");
        __doPostBack("<%= DropDownList1.UniqueID %>", "");
    }
</script>


<asp:DropDownList ID="DropDownList1" runat="server" onchange="xx()">
    <asp:ListItem>q</asp:ListItem>
    <asp:ListItem>w</asp:ListItem>
    <asp:ListItem>e</asp:ListItem>
    <asp:ListItem></asp:ListItem>
</asp:DropDownList>

Code behind(VB.NET)

Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    MsgBox("Do whatever you want here")
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM