javascript onchange event to asp.net dropdownlist

28,930

Solution 1

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 e.g. (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!");
      }
    });
  });
});

Solution 2

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
Share:
28,930
Anju Thapa
Author by

Anju Thapa

Updated on November 28, 2020

Comments

  • Anju Thapa
    Anju Thapa over 3 years

    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>
    
    • Dan Davies Brackett
      Dan Davies Brackett over 12 years
      I'm not totally sure what you're asking here: are you trying to suppress the existing behaviour (which is an autopostback) when the dropdown's value changes? or are you trying to also open the new window?
    • Anju Thapa
      Anju Thapa over 12 years
      I am trying to do the postback but with URL appended as ?key=whateverselected
  • Christopher Marshall
    Christopher Marshall over 12 years
    Whoops, totally assumed you were using Jquery. sorry.
  • Christopher Marshall
    Christopher Marshall over 12 years
    Switched it from a click event to a change event.
  • matskn
    matskn almost 12 years
    Shouldnt you have url : .. ? And not = .. ?