简体   繁体   中英

How do I set previously selected radio button checked in wizard control?

I am going to build online examination system in asp.net using c#.I have created a wizard for online exam but whenever I go to next step and then come back to previous step what I found is the selected radiobuttonr remain unselected. Why this is happening? and what is the solution?

One more question I want to ask :- can I place timer, exam name and total marks at the top of every step without repeating it in every step in wizard control? If yes,then how?

Here is the .cs code of first step of wizard :-

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;
using System.Data;
using System.Configuration;

public partial class Demo : System.Web.UI.Page
{
SqlConnection con;

public Demo()
{
    con = new SqlConnection();
    con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

}


protected void Page_Load(object sender, EventArgs e)
{

}
protected void wizard1(Object sender, System.EventArgs e)
{
    SqlDataAdapter adp = new SqlDataAdapter("select top 10 Question,Option1,Option2,Option3,Option4 from Questions", con);
    DataTable dt = new DataTable();
    adp.Fill(dt);
    Repeater1.DataSource = dt;
    Repeater1.DataBind();


}
}

Here is the .aspx code :-

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo.aspx.cs" Inherits="Demo" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Wizard ID="Wizard1" runat="server" BackColor="#E6E2D8" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" Height="433px" Width="581px"> <HeaderStyle BackColor="#666666" BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="2px" Font-Bold="True" Font-Size="0.9em" ForeColor="White" HorizontalAlign="Center" /> <NavigationButtonStyle BackColor="White" BorderColor="#C5BBAF" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#1C5E55" /> <SideBarButtonStyle ForeColor="White" /> <SideBarStyle BackColor="#1C5E55" Font-Size="0.9em" VerticalAlign="Top" /> <StepStyle BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="2px" /> <WizardSteps> <asp:WizardStep runat="server" Title="Instructions"> Click next and start your exam!!! </asp:WizardStep> <asp:WizardStep ID="WizardStep1" runat="server" Title="Reasoning" OnActivate="wizard1"> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <table border=1> <tr> <td> <asp:Label ID="Label1" runat="server" Text="1"></asp:Label> </td> <td> <%#DataBinder.Eval(Container.DataItem,"Question") %> </td> </tr> <tr> <td></td> <td><asp:RadioButton ID="RadioButton1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Option1")%>' /></td> </tr> <tr> <td></td> <td><asp:RadioButton ID="RadioButton2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Option2")%>' /></td> </tr> <tr> <td></td> <td><asp:RadioButton ID="RadioButton3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Option3")%>' /></td> </tr> <tr> <td></td> <td><asp:RadioButton ID="RadioButton4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Option4")%>' /></td> </tr> </ItemTemplate> </asp:Repeater> </asp:WizardStep> <asp:WizardStep ID="WizardStep2" runat="server" Title="Quantitative Aptitude"></asp:WizardStep> <asp:WizardStep runat="server" Title="English"> </asp:WizardStep> <asp:WizardStep runat="server" Title="Mathematics"> </asp:WizardStep> <asp:WizardStep runat="server" Title="Computer Concepts"> </asp:WizardStep> </WizardSteps> </asp:Wizard> </div> </form> </body> </html> 

There is no need of "protected void wizard1(Object sender, System.EventArgs e)".Remove it.

Create a fill() method and bind data in that function.

 public void fill()
    {
        SqlDataAdapter adp = new SqlDataAdapter("select top 10 Question,Option1,Option2,Option3,Option4 from Questions", con);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
    }

And very important in Page_load add condition

        if (!IsPostBack)
        {
            fill();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM