Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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">
Posted
Updated 1-Apr-11 2:12am
v3
Comments
BobJanova 1-Apr-11 8:42am    
This is an ASPX question, not a C# one. I would guess asp.RadioButton has a property Checked or Selected or similar that you can check.
Member 13424123 22-Oct-17 11:36am    
add aspx.cs page for above aspx page

how to calculate the number of checked radio buttons in different group radio buttons
 
Share this answer
 
Instead of using groups of radiobuttons, why not use RadioButtonList?

XML
<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.
 
Share this answer
 
In desktop application we do...
-------------------------
string chkvalue = rbd_male.checked ? "Male" : "Female";

-------------------------
 
Share this answer
 
Comments
Member 10296279 4-Feb-14 0:12am    
how to calculate the number of checked radio buttons in different group radio buttons
Member 10296279 4-Feb-14 6:21am    
how to assign a string value from radio button to mysql data column and to compare that string with another data column in mysql using asp.net c#?
thanx guys
now the problem is that i am getting the selected value but i 've to calculate and show the result in another.so can u give me some idea how to calculate marks. here is my code behind........

using System.Data.SqlClient;

public partial class client_Default : System.Web.UI.Page
{
//SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["projectConnectionString"].ConnectionString);
int a, b, c, d, addition;
protected void Page_Load(object sender, EventArgs e)
{

}


protected void btnSubmitExam_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvExam.Rows)
{
if (((RadioButton)gvr.FindControl("rbOption1")).Checked == true)
{

if((((RadioButton)gvr.FindControl("rbOption1")).Text) == (gvr.Cells[2].Text))
{
a = 1;
//Response.Write(a);
}
else
{
a = 0;
//Response.Write(a);
}


}
else if (((RadioButton)gvr.FindControl("rbOption2")).Checked == true)
{
if ((((RadioButton)gvr.FindControl("rbOption2")).Text) == (gvr.Cells[2].Text))
{
b = a+1;
//Response.Write(b);

}
else
{
b = 0;
//Response.Write(b);
}
}
else if (((RadioButton)gvr.FindControl("rbOption3")).Checked == true)
{
if ((((RadioButton)gvr.FindControl("rbOption3")).Text) == (gvr.Cells[2].Text))
{
c = b+1;
//Response.Write(c);
}
else
{
c = 0;
//Response.Write(c);
}

}
else if (((RadioButton)gvr.FindControl("rbOption4")).Checked == true)
{
if ((((RadioButton)gvr.FindControl("rbOption4")).Text) == (gvr.Cells[2].Text))
{
d = c+1;
Response.Write(d);

}
else
{
d = 0;
//Response.Write(d);
}

}



}



}


}
 
Share this answer
 
v2
Comments
Toniyo Jackson 6-Apr-11 5:57am    
Don't ask your comment in answer section. Write the comments below the answer.
vikasvanvi 11-Feb-14 5:11am    
How you got radiobutton selected while changing pages in gridview??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900