如何制作多个radiobuttonlists,因此每组只能检查一个按钮

时间:2016-01-18 20:30:37

标签: asp.net vb.net visual-studio-2012

有没有办法确保用户从(按钮列表)中选择单选按钮只能从每个组中选择一个?我有一个网页表单,根据重量,城市和运输方式计算运费。我的代码还没有设计,但这是我尝试的要点

地点每天交货5天每磅2天交货 孟菲斯$ 1.50 $ 2.35 亚特兰大$ 2.75 $ 4.50

在我的表单中,用户点击Memphis然后按下与亚特兰大相对应的按钮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

2 个答案:

答案 0 :(得分:1)

不是100%肯定你在问什么,因为RadioButtonList控件只允许选择一个选项 - 这就是它们与CheckBoxList控件的区别。您是说您只想在所有运输选项中进行一次选择吗?从中可以看出,用户在从“5天交货”中选择某些商品后,无法从“次日送达”中选择一个选项?

如果是这样,请将AutoPostBack="true"OnSelectedIndexChanged处理程序添加到RadioButtonList控件中,然后使用VB代码(RadioButtonList1.ClearSelection())取消选择以前的任何选项点击。

答案 1 :(得分: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