2016-01-18 26 views
0

有沒有一種方法可以確保用戶從(按鈕列表)中選擇單選按鈕只能從每個組中選擇一個按鈕?我有一個網頁表單,可以根據重量,城市和運輸方式計算運費。我的代碼是不是風格還沒有,但是這是我試圖每磅 孟菲斯$ $ 1.50 2.35 亞特蘭大每磅2天交付如何製作多個單選按鈕列表,以便每個組只能檢查一個按鈕

位置5天交貨的要點$ 2.75 $ 4.50

在我的形式向用戶點擊孟菲斯,然後點擊與亞特蘭大對應的按鈕5天,項目權重乘以2.75美元,並將最終成本發送到文本框。現在,用戶可以選擇所有的單選按鈕,但是它們應該在整個表單中對齊。見下面

<ASP Code> 
<%@ Page Language="VB" AutoEventWireup="false" 
CodeFile="Default.aspx.vb" Inherits="_Default" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div>  
    Please select the appropriate shipping information</div> 
    <div> 
     <br /> 
     Weight of Item 
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
&nbsp;&nbsp; 
     <br /> 
     <br /> 
     <br /> 
     Ship To Location 
     <br /> 
     <br /> 
     <asp:RadioButtonList ID="RadioButtonList1" runat="server"> 
      <asp:ListItem>Memphis</asp:ListItem> 
      <asp:ListItem>Cincinnati</asp:ListItem> 
      <asp:ListItem>San Francisco</asp:ListItem> 
      <asp:ListItem>Las Vegas</asp:ListItem> 
      <asp:ListItem>New York</asp:ListItem> 
     </asp:RadioButtonList> 
     <br /> 
     Cost Per Pound 5-day delivery<br /> 
     <br /> 
     <asp:RadioButtonList ID="RadioButtonList2" runat="server"> 
      <asp:ListItem>$1.12</asp:ListItem> 
      <asp:ListItem>$0.92</asp:ListItem> 
      <asp:ListItem>$2.45</asp:ListItem> 
      <asp:ListItem>$1.21</asp:ListItem> 
      <asp:ListItem>$2.00</asp:ListItem> 
     </asp:RadioButtonList> 
     <br /> 
     Cost Per Pound 2-day delivery<br /> 
     <asp:RadioButtonList ID="RadioButtonList3" runat="server"> 
      <asp:ListItem>$1.65</asp:ListItem> 
      <asp:ListItem>$1.45</asp:ListItem> 
      <asp:ListItem>$2.70</asp:ListItem> 
      <asp:ListItem>$1.41</asp:ListItem> 
      <asp:ListItem>$2.70</asp:ListItem> 
     </asp:RadioButtonList> 
     <br /> 
     Cost Per Pound Next day delivery<br /> 
     <asp:RadioButtonList ID="RadioButtonList4" runat="server"> 
      <asp:ListItem>$1.92</asp:ListItem> 
      <asp:ListItem>$1.88</asp:ListItem> 
      <asp:ListItem>$3.00</asp:ListItem> 
      <asp:ListItem>$1.87</asp:ListItem> 
      <asp:ListItem>$2.99</asp:ListItem> 
     </asp:RadioButtonList> 
     <br /> 
     <br /> 
     <asp:Button ID="Button1" runat="server" Text="Calculate 
Shipping Costs" /> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:Button ID="Button2" runat="server" Text="Clear Selections" 
    /> 
     <br /> 
     <br /> 
     <br /> 
     Total Shipping Costs:&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
     <br /> 

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    </div> 
    </form> 
    </body> 
    </html> 

<VB Code> 
Partial Class _Default 
Inherits System.Web.UI.Page 
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles 
Button1.Click 
    'variables to hold weight and shipping cost 
    Dim itemWeight, shipCost As Double 
    'get itemWeight from text box 
    itemWeight = Val(TextBox1.Text) 

    'memphis shipping 
    If RadioButtonList1.SelectedIndex = 0 And 
     RadioButtonList2.SelectedIndex = 0 Then 
     shipCost = (itemWeight * 1.12) 
     TextBox2.Text = shipCost 
    ElseIf RadioButtonList1.SelectedIndex = 0 And 
     RadioButtonList3.SelectedIndex = 0 Then 
     shipCost = (itemWeight * 1.65) 
     TextBox2.Text = shipCost 
    ElseIf RadioButtonList1.SelectedIndex = 0 And 
     RadioButtonList4.SelectedIndex = 0 Then 
     shipCost = (itemWeight * 1.92) 
     TextBox2.Text = shipCost 
    End If 
    'cincinnati shipping 
    If RadioButtonList1.SelectedIndex = 1 And 
     RadioButtonList2.SelectedIndex = 1 Then 
     shipCost = (itemWeight * 0.92) 
     TextBox2.Text = shipCost 
    ElseIf RadioButtonList1.SelectedIndex = 1 And 
     RadioButtonList3.SelectedIndex = 1 Then 
     shipCost = (itemWeight * 1.45) 
     TextBox2.Text = shipCost 
    ElseIf RadioButtonList1.SelectedIndex = 1 And 
     RadioButtonList4.SelectedIndex = 1 Then 
     shipCost = (itemWeight * 1.88) 
     TextBox2.Text = shipCost 
    End If 
    'San Francisco Shipping 
    If RadioButtonList1.SelectedIndex = 2 And 
     RadioButtonList2.SelectedIndex = 2 Then 
     shipCost = (itemWeight * 2.45) 
     TextBox2.Text = shipCost 
    ElseIf RadioButtonList1.SelectedIndex = 2 And 
     RadioButtonList3.SelectedIndex = 2 Then 
     shipCost = (itemWeight * 2.7) 
     TextBox2.Text = shipCost 
    ElseIf RadioButtonList1.SelectedIndex = 2 And 
     RadioButtonList4.SelectedIndex = 2 Then 
     shipCost = (itemWeight * 3.0) 
     TextBox2.Text = shipCost 
    End If 
    'Las Vegas Shipping 
    If RadioButtonList1.SelectedIndex = 3 And 
     RadioButtonList2.SelectedIndex = 3 Then 
     shipCost = (itemWeight * 1.21) 
     TextBox2.Text = shipCost 
    ElseIf RadioButtonList1.SelectedIndex = 3 And 
     RadioButtonList3.SelectedIndex = 3 Then 
     shipCost = (itemWeight * 1.41) 
     TextBox2.Text = shipCost 
    ElseIf RadioButtonList1.SelectedIndex = 3 And 
     RadioButtonList4.SelectedIndex = 4 Then 
     shipCost = (itemWeight * 1.87) 
     TextBox2.Text = shipCost 
    End If 
    'New York Shipping 
    If RadioButtonList1.SelectedIndex = 4 And 
     RadioButtonList2.SelectedIndex = 4 Then 
     shipCost = (itemWeight * 2.0) 
     TextBox2.Text = shipCost 
    ElseIf RadioButtonList1.SelectedIndex = 4 And 
     RadioButtonList3.SelectedIndex = 4 Then 
     shipCost = (itemWeight * 2.7) 
     TextBox2.Text = shipCost 
    ElseIf RadioButtonList1.SelectedIndex = 4 And 
     RadioButtonList4.SelectedIndex = 4 Then 
     shipCost = (itemWeight * 2.99) 
     TextBox2.Text = shipCost 
    End If 

End Sub 

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles 

Button2.Click 
    For Each ctrl As Control In form1.Controls 
     If ctrl.[GetType]() = GetType(TextBox) Then 
      DirectCast(ctrl, TextBox).Text = String.Empty 
     ElseIf ctrl.[GetType]() = GetType(RadioButtonList) Then 
      DirectCast(ctrl, RadioButtonList).ClearSelection() 
     End If 
    Next 
End Sub 
End Class 

回答

1

不是100%肯定的代碼,你要問什麼,因爲RadioButtonList控制只允許選擇一個選項時無論如何,這就是從CheckBoxList控制區分它們。你的意思是你只想在所有運送選項中允許一個選項?如在用戶從「5天發貨」中選擇某些東西后,應該無法從「次日發貨」中選擇一個選項?

如果是這樣,添加AutoPostBack="true"OnSelectedIndexChanged處理的RadioButtonList控件,然後使用VB代碼(RadioButtonList1.ClearSelection())點擊一個新的時取消選擇任何以前的選擇。

+0

是的,沒錯。他們可以通過選擇另一個選項來改變選擇。 – allendks45

0

我建議使用位於「標準」組中的工具箱中的DropDownList控件,並將這些項目設置爲「(選擇)」,「5天遞送」,「2天遞送」,「下一天交貨「而不是3組RadioButtonList交貨類型。這裏是你可以做的,開始從你的源代碼提供給我們什麼樣的例子:

ASP.NET代碼:

<%@ Page Language="VB" AutoEventWireup="false" 
CodeFile="Default.aspx.vb" Inherits="_Default" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div>  
    Please select the appropriate shipping information</div> 
    <div> 
     <br /> 
     Weight of Item:&nbsp; 
     <asp:TextBox ID="txtItemWeight" runat="server"></asp:TextBox> 
&nbsp;&nbsp; 
     <br /> 
     <br /> 
     <br /> 
     Ship To Location 
     <br /> 
     <br /> 
     <asp:RadioButtonList ID="radCity" runat="server"> 
      <asp:ListItem>Memphis</asp:ListItem> 
      <asp:ListItem>Cincinnati</asp:ListItem> 
      <asp:ListItem>San Francisco</asp:ListItem> 
      <asp:ListItem>Las Vegas</asp:ListItem> 
      <asp:ListItem>New York</asp:ListItem> 
     </asp:RadioButtonList> 
     <br /> 
     Delivery Type:<br /> 
     <asp:DropDownList ID="ddlDeliveryType" runat="server"> 
      <asp:ListItem>(select)</asp:ListItem> 
      <asp:ListItem>5-Day Delivery</asp:ListItem> 
      <asp:ListItem>2-Day Delivery</asp:ListItem> 
      <asp:ListItem>Next Day Delivery</asp:ListItem> 
     </asp:DropDownList> 
     <br /> 
     <br /> 



     <asp:Button ID="btnCalculate" runat="server" Text="Calculate 
Shipping Costs" /> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:Button ID="btnClear" runat="server" Text="Clear Selections" 
    /> 
     <br /> 
     <br /> 
     <br /> 
     Total Shipping Costs:&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="txtShippingCost" runat="server" ReadOnly="True" Width="160px"></asp:TextBox> 
     <br /> 

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    </div> 
    </form> 
    </body> 
    </html> 

Visual Basic代碼:

Partial Class _Default 
    Inherits System.Web.UI.Page 

    Protected Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click 

     Dim dblItemWeight As Double 
    Dim dblDeliveryCost As Double 

     If Double.TryParse(txtItemWeight.Text, dblItemWeight) Then 

      If ddlDeliveryType.SelectedIndex <> 0 Then 

       ' 5-Day delivery 
       If ddlDeliveryType.SelectedIndex = 1 Then 

        If radCity.SelectedIndex = 0 Then 
         dblDeliveryCost = dblItemWeight * 1.12 
        ElseIf radCity.SelectedIndex = 1 Then 
         dblDeliveryCost = dblItemWeight * 0.92 
        ElseIf radCity.SelectedIndex = 2 Then 
         dblDeliveryCost = dblItemWeight * 2.45 
        ElseIf radCity.SelectedIndex = 3 Then 
         dblDeliveryCost = dblItemWeight * 1.21 
        ElseIf radCity.SelectedIndex = 4 Then 
         dblDeliveryCost = dblItemWeight * 2.0 

        End If 

       End If 

       ' 2-Day delivery 
       If ddlDeliveryType.SelectedIndex = 2 Then 

        If radCity.SelectedIndex = 0 Then 
         dblDeliveryCost = dblItemWeight * 1.65 
        ElseIf radCity.SelectedIndex = 1 Then 
         dblDeliveryCost = dblItemWeight * 1.45 
        ElseIf radCity.SelectedIndex = 2 Then 
         dblDeliveryCost = dblItemWeight * 2.7 
        ElseIf radCity.SelectedIndex = 3 Then 
         dblDeliveryCost = dblItemWeight * 1.41 
        ElseIf radCity.SelectedIndex = 4 Then 
         dblDeliveryCost = dblItemWeight * 2.7 

        End If 

       End If 

       ' Next Day delivery 
       If ddlDeliveryType.SelectedIndex = 3 Then 

        If radCity.SelectedIndex = 0 Then 
         dblDeliveryCost = dblItemWeight * 1.92 
        ElseIf radCity.SelectedIndex = 1 Then 
         dblDeliveryCost = dblItemWeight * 1.88 
        ElseIf radCity.SelectedIndex = 2 Then 
         dblDeliveryCost = dblItemWeight * 3.0 
        ElseIf radCity.SelectedIndex = 3 Then 
         dblDeliveryCost = dblItemWeight * 1.87 
        ElseIf radCity.SelectedIndex = 4 Then 
         dblDeliveryCost = dblItemWeight * 2.99 

        End If 

       End If 

       txtShippingCost.Text = dblDeliveryCost.ToString("c") 

      End If 

     Else 
      txtShippingCost.Text = "Please only enter numbers." 
     End If 
    End Sub 

Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click 

     For Each ctrl As Control In form1.Controls 
      If ctrl.[GetType]() = GetType(TextBox) Then 
       DirectCast(ctrl, TextBox).Text = String.Empty 
      ElseIf ctrl.[GetType]() = GetType(RadioButtonList) Then 
       DirectCast(ctrl, RadioButtonList).ClearSelection() 
      End If 
     Next 

     ddlDeliveryType.SelectedIndex = 0 

    End Sub 
    End Class 
+0

我真的希望使用單選按鈕,然後使用下拉列表來播放我正在處理的另一個表單。 – allendks45

相關問題