如何获得radiobutton的选定值。 [英] how to get selected value of radiobutton.

查看:77
本文介绍了如何获得radiobutton的选定值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨每一个



我正在制作在线考试系统。



我有疑问和四个选项在网格视图中,所以我想得到单选按钮的选定值并计算并显示在结果页面中你可以帮助我吗?



这是我的代码...



<%@ Page Language =C#MasterPageFile =〜/ client / MasterPage.masterAutoEventWireup =true CodeFile =examination.aspx.csInherits =client_DefaultTitle =Untitled Page%> 

< asp:Content ID =Content1ContentPlaceHolderID =headRunat =Server>
< div style =width:100%; text-align:center; font-size:16pt; font-weight:bold; float:left;>
考试页面
< / div>
< div style =width:100%;>
< asp:GridView ID =gvExamrunat =serverAutoGenerateColumns =False
DataSourceID =SqlDataSource1
DataKeyNames =questionid,question,option1,option2,option3,选项4,回答>
< columns>
< asp:BoundField DataField =questionidHeaderText =questionid
InsertVisible =FalseReadOnly =TrueSortExpression =questionid
Visible =False/>
< asp:TemplateField HeaderText =questionSortExpression =question>
< itemtemplate>
< table style =width:1000px;>
< tr>
< td>< asp:Label ID =Label1runat =serverText ='<%#Bind(question)%>'>< / td>
< / tr>
< tr>
< td style =padding-removed 20px;>
< asp:RadioButton ID =rbOption1runat =serverText ='<%#Bind(Option1)%>'GroupName ='<%#Bind(questionid)% >'/>
< / td>
< / tr>
< tr>
< td style =padding-removed 20px;>
< asp:RadioButton ID =rbOption2runat =serverText ='<%#Bind(Option2)%>'GroupName ='<%#Bind(questionid)% >'/>
< / td>
< / tr>
< tr>
< td style =padding-removed 20px;>
< asp:RadioButton ID =rbOption3runat =serverText ='<%#Bind(Option3)%>'GroupName ='<%#Bind(questionid)% >'/>
< / td>
< / tr>
< tr>
< td style =padding-removed 20px;>
< asp:RadioButton ID =rbOption4runat =serverText ='<%#Bind(Option4)%>'GroupName ='<%#Bind(questionid)% >'/>
< / td>
< / tr>
< / table>
< / itemtemplate>

< asp:BoundField DataField =answerHeaderText =answer
SortExpression =answerVisible =False/>
< asp:BoundField DataField =subidHeaderText =subidSortExpression =subid
Visible =False/>
< / columns>

< asp:SqlDataSource ID =SqlDataSource1runat =server
ConnectionString =<%$ ConnectionStrings:projectConnectionString%>
SelectCommand =SELECT * FROM [questionmaster] WHERE([subid] = @subid)>
< SelectParameters>
< asp:QueryStringParameter Name =subidQueryStringField =sidType =Int32/>
< / SelectParameters>

< asp:Button ID =btnSubmitExamrunat =serveronclick =btnSubmitExam_Click
Text =Submit/>
< asp:Label ID =lblselectedrunat =serverText =Label>
< / div>

< asp:Content ID =Content2ContentPlaceHolderID =ContentPlaceHolder1Runat =Server>

解决方案

ConnectionStrings:projectConnectionString%>
SelectCommand =SELECT * FROM [questionmaster] WHERE([subid] = @subid)>
< SelectParameters>
< asp:QueryStringParameter Name =subidQueryStringField =sidType =Int32/>
< / SelectParameters>

< asp:Button ID =btnSubmitExamrunat = serveronclick =btnSubmitExam_Click
Text =Submit/>
< asp:Label ID =lblselectedrunat =serverText =Label>
< / div>

< asp:Content ID =Content2ContentPlaceHolderID =ContentPlaceHolder1Runat =Server>


为什么不使用 RadioButtonList



 <   asp:RadioButtonList     ID   =  rBtnList1    runat   =  server   

RepeatColumns = 1

RepeatDirection = 垂直

RepeatLayout = >
< asp:ListItem 文字 = Text1 = Value1 > < / asp :ListItem >
< asp:ListItem 文字 = Text2 < span class =code-keyword> = Value2 > < / asp:ListItem >
< asp:ListItem 文本 = Text3 = Value3 > < ; / asp:ListItem >
< asp:ListItem 文字 = Text4 = Value4 > < / asp:ListItem >
< / asp:RadioButtonList >





您可以通过 rBtnList1.SelectedValue 访问所选值。

RepeatColumns指示为表创建的列数有等属性可用于设置输出表的外观。通过正确使用此控件,您不再需要在每个单选按钮上添加样式属性,就像您在表的每个td中所做的那样。如果以这种方式实现它看起来更干净。


在桌面应用程序中我们做...

-------------- -----------

 string chkvalue = rbd_male.checked? 男:女; 



-------------------------


hi every

I am making online examination system.

I have question and four options inside a grid view so I want to get the selected value of the radio button and calculate and show that in the result page can you help me?

Here is my code...

<%@ Page Language="C#" MasterPageFile="~/client/MasterPage.master" AutoEventWireup="true" CodeFile="examination.aspx.cs" Inherits="client_Default" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <div style="width: 100%; text-align: center; font-size: 16pt; font-weight: bold; float: left;">
        Examination Page
    </div>
    <div style="width: 100%;">
        <asp:GridView ID="gvExam" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1" 
            DataKeyNames="questionid,question,option1,option2,option3,option4,answer">
            <columns>
                <asp:BoundField DataField="questionid" HeaderText="questionid" 
                    InsertVisible="False" ReadOnly="True" SortExpression="questionid" 
                    Visible="False" />
                <asp:TemplateField HeaderText="question" SortExpression="question">
                    <itemtemplate>
                        <table style="width: 1000px;">
                            <tr>
                                <td><asp:Label ID="Label1" runat="server" Text='<%# Bind("question") %>'></td>
                            </tr>
                            <tr>
                                <td style="padding-removed 20px;">
                                    <asp:RadioButton ID="rbOption1" runat="server" Text = '<%# Bind("Option1") %>' GroupName= '<%# Bind("questionid") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td style="padding-removed 20px;">
                                    <asp:RadioButton ID="rbOption2" runat="server" Text = '<%# Bind("Option2") %>' GroupName= '<%# Bind("questionid") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td style="padding-removed 20px;">
                                    <asp:RadioButton ID="rbOption3" runat="server" Text = '<%# Bind("Option3") %>' GroupName= '<%# Bind("questionid") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td style="padding-removed 20px;">
                                    <asp:RadioButton ID="rbOption4" runat="server" Text = '<%# Bind("Option4") %>' GroupName= '<%# Bind("questionid") %>' />
                                </td>
                            </tr>
                        </table>
                    </itemtemplate>
                
                <asp:BoundField DataField="answer" HeaderText="answer" 
                    SortExpression="answer" Visible="False" />
                <asp:BoundField DataField="subid" HeaderText="subid" SortExpression="subid" 
                    Visible="False" />
            </columns>
        
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:projectConnectionString %>" 
            SelectCommand="SELECT * FROM [questionmaster] WHERE ([subid] = @subid)">
            <SelectParameters>
                <asp:QueryStringParameter Name="subid" QueryStringField="sid" Type="Int32" />
            </SelectParameters>
        
        <asp:Button ID="btnSubmitExam" runat="server" onclick="btnSubmitExam_Click" 
            Text="Submit"  />
        <asp:Label ID="lblselected" runat="server" Text="Label">
    </div>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

解决方案

ConnectionStrings:projectConnectionString %>" SelectCommand="SELECT * FROM [questionmaster] WHERE ([subid] = @subid)"> <SelectParameters> <asp:QueryStringParameter Name="subid" QueryStringField="sid" Type="Int32" /> </SelectParameters> <asp:Button ID="btnSubmitExam" runat="server" onclick="btnSubmitExam_Click" Text="Submit" /> <asp:Label ID="lblselected" runat="server" Text="Label"> </div> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


Instead of using groups of radiobuttons, why not use RadioButtonList?

<asp:RadioButtonList ID="rBtnList1" runat="server"

        RepeatColumns = "1"

        RepeatDirection="Vertical"

        RepeatLayout="Table">
            <asp:ListItem Text="Text1" Value="Value1"></asp:ListItem>
            <asp:ListItem Text="Text2" Value="Value2"></asp:ListItem>
            <asp:ListItem Text="Text3" Value="Value3"></asp:ListItem>
            <asp:ListItem Text="Text4" Value="Value4"></asp:ListItem>
        </asp:RadioButtonList>



You can access the selected value by rBtnList1.SelectedValue.
RepeatColumns indicate how many colums are being created for the table and etc. There are properties for setting the appearance of the output table. By making proper use of this control, you no longer have to put a style attribute on every radio button, like what you have done in every td of your table. Looks cleaner if it were implemented this way.


In desktop application we do...
-------------------------

string chkvalue = rbd_male.checked ? "Male" : "Female";


-------------------------


这篇关于如何获得radiobutton的选定值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆