如何在aspx中获得单选按钮的值?

时间:2016-06-10 12:11:46

标签: html asp.net

有人可以帮助我,我有这个代码,它应该投票,我不知道如何获得按钮的价值并计算它。 感谢所有帮手:) 所以这是我的代码:

  <input type="radio" name="group1" value="Call Of Duty" />Call Of Duty
        <input type="radio" name="group1" value="Battlefield" />Battlefield
        <input type="radio" name="group1" value="Assassin's creed" />Assassin's creed
        <input type="radio" name="group1" value="Minecraft" />Minecraft
        <input type="radio" name="group1" value="Fifa" />Fifa<br />
        <br /><input type="submit" value="vote">

1 个答案:

答案 0 :(得分:0)

如果您在.Net工作,我建议您使用asp:RadioButtonList此控件将帮助您以更好的方式检索它背后的代码中的值。这是一个例子:

关于Default.aspx文件:

<form id="form1" runat="server">
    <asp:RadioButtonList ID="RBLVote" runat="server">
        <asp:ListItem Value="1">Call of duty</asp:ListItem>
        <asp:ListItem Value="2">Battlefield</asp:ListItem>
        <asp:ListItem Value="3">Assassin's Creed</asp:ListItem>
        <asp:ListItem Value="4">Minecraft</asp:ListItem>
        <asp:ListItem Value="5">FIFA</asp:ListItem>
    </asp:RadioButtonList>
    <br /><br />
    <asp:Label ID="LResult" runat="server"></asp:Label>
    <br /><br />
    <asp:LinkButton ID="btnGo" runat="server">Vote!</asp:LinkButton>
</form>

在Default.aspx.vb文件上(如果你在.cs上需要这个代码,你可以使用像converter.telerik.com这样的代码转换器来完成这项工作):

Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGo.Click

    Try

        If RBLVote.SelectedValue = "1" Then
            LResult.Text = "You voted for Call of duty"
        End If

        If RBLVote.SelectedValue = "2" Then
            LResult.Text = "You voted for Battlefield"
        End If

        If RBLVote.SelectedValue = "3" Then
            LResult.Text = "You voted for Assassin's Creed"
        End If

        If RBLVote.SelectedValue = "4" Then
            LResult.Text = "You voted for Minecraft"
        End If

        If RBLVote.SelectedValue = "5" Then
            LResult.Text = "You voted for FIFA"
        End If

        If RBLVote.SelectedValue.Length = "0" Then
            LResult.Text = "You must select an option!"
        End If
        'You can place here other actions, like the code to save to your database.

    Catch ex As Exception
        LResult.Text = "There was a problem processing your vote, please trt again later.<br/><br/>" & ex.ToString
    End Try

End Sub

我希望这可以帮到你!