簡體   English   中英

javascript onchange事件到asp.net下拉列表

[英]javascript onchange event to asp.net dropdownlist

是否有可能使下拉列表選擇觸發器通過使用javascript作為查詢字符串添加到url的任何選擇而回發到同一頁面? 問題是該列表是從某個共享點列表中動態加載的。 如果我的網站是mysite.com/default.aspx,那么在進行選擇時,它應該重定向到mysite.com/default.aspx?key=selection

無法訪問服務器,無法訪問代碼隱藏:(

 <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;
});

未經測試,也不確定哪個事件正在重新加載頁面,例如(提交或定位)

來自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!");
      }
    });
  });
});

我不確定您要做什么,但是我想jquery是您問題的正確答案

無論如何,這可能會有所幫助:

<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>

背后的代碼(VB.NET)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM