1

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>
2
  • 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? Commented Jan 5, 2012 at 22:08
  • I am trying to do the postback but with URL appended as ?key=whateverselected Commented Jan 5, 2012 at 22:25

2 Answers 2

7
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 could be:

$(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!");
      }
    });
  });
});
Sign up to request clarification or add additional context in comments.

3 Comments

Whoops, totally assumed you were using Jquery. sorry.
Switched it from a click event to a change event.
Shouldnt you have url : .. ? And not = .. ?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.