2013-01-04 121 views
0

我是一個非常新的Visual Studio 2010.我有一個下拉列表,從基本select語句中提取數據列表。下拉列表中只有4種選擇。假設它只是蘋果,香蕉,梨和葡萄。我如何做到這一點,當我選擇蘋果它重定向到一個頁面,但如果我選擇香蕉它重定向到另一個?目前我有提交按鈕指向一個頁面,但我如何使它可以根據用戶選擇動態更改?ASP.net在Visual STudio 2010.使用下拉列表和提交按鈕

回答

2

處理選擇元素的onchange事件:

function getComboA(sel) { 
    var value = sel.options[sel.selectedIndex].value; 
    // decide what to do with value for example 
    if(value=='apple') 
     window.location='pages.aspx'; 

} 

<asp:dropDownList onchange="getComboA(this);" ... 
0

這個時候,你不需要提供任何控制ASP。

選項1

<select id="myselect" onchange="myfunction();"> 
    <option value=""></option> 
    <option value="apple">apple</option> 
    <option value="banana">banana</option> 
    <option value="pear">pear</option> 
    <option value="grapes">grapes</option> 
</select> 
<script type="text/javascript"> 
    function myfunction() { 
     var value = $("#myselect").val(); 
     switch (value) { 
      case "apple": 
       window.location = "http://www.apple.com/"; 
       break; 
      case "banana": 
       window.location = "http://www.google.com/"; 
       break; 
      case "pear": 
       window.location = "http://www.apple.com/"; 
       break; 
      case "grapes": 
       window.location = "http://www.apple.com/"; 
       break; 
     } 
    } 
</script> 

選項2

<asp:DropDownList ID="myselect" onchange="myfunction();" runat="server"> 
    <asp:ListItem Text="apple" Value="apple" /> 
    <asp:ListItem Text="banana" Value="banana" /> 
    <asp:ListItem Text="pear" Value="pear" /> 
    <asp:ListItem Text="grapes" Value="grapes" /> 
</asp:DropDownList> 
<script type="text/javascript"> 
    function myfunction() { 
     var value = $("#<%= myselect.ClientID %>").val(); 
     switch (value) { 
      case "apple": 
       window.location = "http://www.apple.com/"; 
       break; 
      case "banana": 
       window.location = "http://www.google.com/"; 
       break; 
      case "pear": 
       window.location = "http://www.apple.com/"; 
       break; 
      case "grapes": 
       window.location = "http://www.apple.com/"; 
       break; 
     } 
    } 
</script> 

選項3(需要回發)中的.cs後面代碼

<asp:DropDownList ID="myselect" runat="server" OnSelectedIndexChanged="myselect_SelectedIndexChanged" AutoPostBack="True"> 
    <asp:ListItem Text="apple" Value="apple" /> 
    <asp:ListItem Text="banana" Value="banana" /> 
    <asp:ListItem Text="pear" Value="pear" /> 
    <asp:ListItem Text="grapes" Value="grapes" /> 
</asp:DropDownList> 

protected void myselect_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    switch (myselect.SelectedValue) 
    { 
     case "apple": 
      Response.Redirect("http://www.apple.com/"); 
      break; 
     case "banana": 
      Response.Redirect("http://www.apple.com/"); 
      break; 
     case "pear": 
      Response.Redirect("http://www.apple.com/"); 
      break; 
     case "grapes": 
      Response.Redirect("http://www.apple.com/"); 
      break; 
    } 
} 
相關問題