2016-07-30 58 views
1

爲什麼當用戶從下拉菜單中選擇不同的選項時,Line1Co.Value始終保持爲0?受保護的Sub name.ValueChanged不更新

這裏是我的HTML:

 <asp:DropDownList ID="HandLine1Size" runat="server" AutoPostBack="true" Width="110px"> 
      <asp:ListItem Value="1">1.75</asp:ListItem> 
      <asp:ListItem Value="1.5625">2</asp:ListItem> 
      <asp:ListItem Value="2.25">2.5</asp:ListItem> 
      <asp:ListItem Value="3.0625">3</asp:ListItem> 
      <asp:ListItem Value="0">No handline</asp:ListItem> 
     </asp:DropDownList 
<asp:HiddenField ID="Line1Co" runat="server" /> 
<br /> 
    <%= Line1Co.Value %> 

這裏是我的代碼背後:

Protected Sub Line1Co_ValueChanged(sender As Object, e As EventArgs) Handles Line1Co.ValueChanged 
    If HandLine1Size.SelectedValue = "1" Then Line1Co.Value = "12" Else 
    If HandLine1Size.SelectedValue = "1.5625" Then Line1Co.Value = "8" Else 
    If HandLine1Size.SelectedValue = "2.25" Then Line1Co.Value = "2" Else 
    If HandLine1Size.SelectedValue = "3.0625" Then Line1Co.Value = "0.75" Else 
    Line1Co.Value = "20" 
End Sub 

回答

0

我看到你的代碼的兩個問題:

  1. 您應該處理的SelectedIndexChanged DropDownList而不是HiddenField的ValueChanged事件。

  2. 單行if陳述對我來說看起來不正確,最後Else。最後一行始終執行,並且在所有情況下都將值設置爲"20"


下面是實現你想要的一些方法。

隨着單行if聲明:

Protected Sub HandLine1Size_SelectedIndexChanged(sender As Object, e As EventArgs) Handles HandLine1Size.SelectedIndexChanged 
    Line1Co.Value = "20" 
    If HandLine1Size.SelectedValue = "1" Then Line1Co.Value = "12" 
    If HandLine1Size.SelectedValue = "1.5625" Then Line1Co.Value = "8" 
    If HandLine1Size.SelectedValue = "2.25" Then Line1Co.Value = "2" 
    If HandLine1Size.SelectedValue = "3.0625" Then Line1Co.Value = "0.75" 
End Sub 

隨着多if聲明:

Protected Sub HandLine1Size_SelectedIndexChanged(sender As Object, e As EventArgs) Handles HandLine1Size.SelectedIndexChanged 
    If HandLine1Size.SelectedValue = "1" Then 
     Line1Co.Value = "12" 
    ElseIf HandLine1Size.SelectedValue = "1.5625" Then 
     Line1Co.Value = "8" 
    ElseIf HandLine1Size.SelectedValue = "2.25" Then 
     Line1Co.Value = "2" 
    ElseIf HandLine1Size.SelectedValue = "3.0625" Then 
     Line1Co.Value = "0.75" 
    Else 
     Line1Co.Value = "20" 
    End If 
End Sub 

隨着Select聲明:

Protected Sub HandLine1Size_SelectedIndexChanged(sender As Object, e As EventArgs) Handles HandLine1Size.SelectedIndexChanged 
    Select Case HandLine1Size.SelectedValue 
     Case "1" 
      Line1Co.Value = "12" 
     Case "1.5625" 
      Line1Co.Value = "8" 
     Case "2.25" 
      Line1Co.Value = "2" 
     Case "3.0625" 
      Line1Co.Value = "0.75" 
     Case Else 
      Line1Co.Value = "20" 
    End Select 
End Sub