Answered by:
Selecting appropriate radio button and displaying it in the next page

Question
-
User441592953 posted
Hello everyone!
I am trying to accept a random row from a table and inserting the values of different columns properly in the appropriate radio button.
Upto this its working fine.
Now I want to check which radio button was selected. Just out of curiosity, I took a session variable and tried to store the selected radiobutton value in it and display it on the next page on the click of a button. But I am getting wrong result for the different click of the submit button.
Please help.
//code for Default2.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server"></asp:Label> <br /> <br /> <asp:RadioButton ID="RadioButton1" runat="server" GroupName="1" /> <br /> <asp:RadioButton ID="RadioButton2" runat="server" GroupName="1" /> <br /> <asp:RadioButton ID="RadioButton3" runat="server" GroupName="1" /> <br /> <asp:RadioButton ID="RadioButton4" runat="server" GroupName="1" /> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /> <br /> <br /> <asp:Label ID="Label2" runat="server"></asp:Label> </div> </form> </body> </html>
//code for Default2.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; public partial class Default2 : System.Web.UI.Page { string option; string cop; string op1; string op2; string op3; string op4; protected void Page_Load(object sender, EventArgs e) { Session["data"] = ""; SqlConnection conn = new SqlConnection(); //creating connection reference object conn.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\ANJAN\Documents\Visual Studio 2013\WebSites\DemoExam\App_Data\Exam.mdf;Integrated Security=True"; conn.Open(); //opening connection string q = "SELECT TOP 1 qid, courseid, Question, op1, op2, op3, op4, cop FROM Questions where courseid='" + 3 + "' ORDER BY NEWID()"; SqlCommand cmd = new SqlCommand(q, conn); //execute query SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read() == true) { string qid= reader.GetValue(0).ToString(); string question = reader.GetValue(2).ToString(); //Session["user"] = "student"; op1 = reader.GetValue(3).ToString(); op2 = reader.GetValue(4).ToString(); op3 = reader.GetValue(5).ToString(); op4 = reader.GetValue(6).ToString(); cop = reader.GetValue(7).ToString(); // Session["course"] = reader.GetValue(4).ToString(); // Session["marks"] = reader.GetValue(6).ToString(); // Response.Redirect("Default.aspx"); Label1.Text = question; RadioButton1.Text = op1; RadioButton2.Text = op2; RadioButton3.Text = op3; RadioButton4.Text = op4; } } protected void Button1_Click(object sender, EventArgs e) { if (RadioButton1.Checked) { option = RadioButton1.Text; } else if (RadioButton2.Checked) { option = RadioButton2.Text; } else if (RadioButton3.Checked) { option = RadioButton3.Text; } else if (RadioButton4.Checked) { option = RadioButton4.Text; } //Label2.Text = option; Session["data"] = option; Response.Redirect("Default.aspx"); //Response.Write("<script>alert("+option+");</script>"); /* if (option == cop) { Response.Write("<script>alert('Correct');</script>"); } else { Response.Write("<script>alert('Incorrect');</script>"); } op1 = ""; op2 = ""; op3 = ""; op4 = ""; cop = ""; */ } }
//code for Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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> <asp:Button ID="Button1" runat="server" PostBackUrl="~/Default2.aspx" Text="Take exam" /> <br /> <asp:Label ID="Label1" runat="server"></asp:Label> </div> </form> </body> </html>
//code for Default.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Label1.Text = Session["data"].ToString(); } }
Saturday, April 23, 2016 5:46 PM
Answers
-
User527778624 posted
Hi,
Try adding logic with in IsPostBack condition in your page_Load() event of default2 page.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, April 24, 2016 7:23 AM
All replies
-
User527778624 posted
Hi,
Try adding logic with in IsPostBack condition in your page_Load() event of default2 page.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, April 24, 2016 7:23 AM -
User441592953 posted
Yes...thank you so much!!
It works
Sunday, April 24, 2016 3:53 PM