Am currently working on my end of semester project (online examination system )where questions are to be obtained from a database. The question is displayed in a label and the four options in radiobuttons.. this form has 3 buttons that can enable a user to navigate to the next question,previuos question(s) and another to intentionally end the exam.whenever a user clicks on the back button of the form to access a previously answered question,the previous question is shown but the radio button that was checked as the answer is not checked.Any idea on how can i maintain the checked status for the previously answered questions?

Dani AI

Generated

This thread shows the classic symptom: the page loads the prior question but the previously selected radio option isn’t re-applied, because the app isn’t persisting the user’s choice when moving between questions. Persist the choice on every navigation, then restore it when the question is rendered. Following ’s suggestion, server-side storage (Session or a DB) is the simplest reliable approach; building on @V’s cookie idea, client storage works for responsiveness but is limited (size, tamperable, can be blocked).

A practical pattern (server-side Session + RadioButtonList) — save on Next/Prev, restore on render:

// Store a map in Session keyed by questionId
void SaveAnswer(int questionId, string choice) {
    var map = Session["QuizAnswers"] as Dictionary<int,string>;
    if (map==null) { map = new Dictionary<int,string>(); Session["QuizAnswers"] = map; }
    map[questionId] = choice;
}

string GetAnswer(int questionId) {
    var map = Session["QuizAnswers"] as Dictionary<int,string>;
    return (map != null && map.TryGetValue(questionId, out var v)) ? v : null;
}

// When rendering:
void RenderQuestion(int questionId) {
    var q = LoadQuestionFromDb(questionId);
    rblOptions.DataSource = q.Options; // RadioButtonList
    rblOptions.DataBind();
    var saved = GetAnswer(questionId);
    if (!string.IsNullOrEmpty(saved)) rblOptions.SelectedValue = saved;
}

If staying with individual radio controls, set each control’s Checked based on the saved value (call the save method before changing questions). Use a DB instead of Session when answers must survive server restarts or support multiple servers; use Session for single-server, short-term storage.

The “ugly” radio visuals are usually CSS from the master page or a reset stylesheet. Inspect the rendered input with browser DevTools, look for rules targeting input[type="radio"] or global font/line-height rules, and remove the many <br/> spacers in favor of CSS margin. A minimal reset rule can help:

input[type="radio"] { width:auto; height:auto; vertical-align:middle; }

For exam integrity prefer server-side storage (DB + audit) rather than cookies/localStorage, and always save the current answer before performing the navigation that shows the next/previous question. If random selection is needed later, ’s approach to selecting without repeats fits well with this storage pattern.

Recommended Answers

All 8 Replies

Keep the answers in database or Session

can you store the test data in a cookie and write the buttons with an if statement? Similar to my post on this page?

http://www.daniweb.com/forums/thread227757.html

If you collect the answers in a cookie as you go, you should be able to redirect based on the cookie parameter. So if the cookie parameter is something like "if cookie[question4] is null or empty then redirect to question 3" ??? This would also allow the user to skip a question and go back to it at anytime with a little more logic.

Thanks 4 ur help... i ll try out using cookies...
thr is one more smaal issue... I am using radiobuttons for d answer options in a contentplaceholder and the size n shape they are taking is as shown in the attachment.It looks ugly this way. I want d normal radiobuttons. Why do they look lik this??? :(

Can you post your code?

<%@ Page Language="C#" MasterPageFile="~/PL/MasterPage2.master" AutoEventWireup="true" CodeFile="../BL/TestQues.aspx.cs" Inherits="PL_TestQues" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <table style="width: 540px; height: 137px">
    
        <tr>
            <td colspan="4" style="font-weight: bold">
                Question
                <asp:Label ID="lblNo" runat="server" Font-Bold="True"></asp:Label>
                of
                <asp:Label ID="lblTotalNo" runat="server" Font-Bold="True"></asp:Label><br />
                <br />
                <asp:Label ID="lblQues" runat="server" Font-Bold="True" Font-Names="Verdana" Font-Size="Small" ></asp:Label>
                <br />
                <br />
                <br />
            </td>
        </tr>
         <tr>
            <td colspan="4" rowspan="2" style="height: 175px">
                <br />
                <br />
                <br />
                <br />
               
                 <asp:RadioButton ID="rbtA" runat="server" GroupName = "grpAns"  Font-Names="Verdana" ForeColor="Black" TabIndex="1"  /><br />
                    <br />
                    <br />
                 <asp:RadioButton ID="rbtB" runat="server" GroupName = "grpAns"  Font-Names="Verdana" ForeColor="Black" Tabndex = "1" />
                    <br />
                    <br />
                 <asp:RadioButton ID="rbtC" runat="server" GroupName = "grpAns"  Font-Names="Verdana" ForeColor="Black" TabIndex="1"  />
                    <br />
                    <br />
                 <asp:RadioButton ID="rbtD" runat="server" GroupName = "grpAns"  Font-Names="Verdana" ForeColor="Black" TabIndex="1"  />
               
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
               
                
                
               
                <br />
            </td>
        </tr> 
        <tr>
        </tr>
        <tr>
            <td colspan="3" rowspan="1" style="width: 350px; height: 37px">
                <asp:Button ID="btnPrev" runat="server" Text="PREVIOUS" OnClick="btnPrev_Click" BackColor="Gray" Font-Bold="True" ForeColor="White" Height="37px" Width="174px"  /></td>
            <td colspan="1" rowspan="1" style="width: 3px; height: 37px">
                <asp:Button ID="btnNext" runat="server" OnClick="btnNext_Click" Text="NEXT" BackColor="Gray" BorderColor="#E3B333" Font-Bold="True" ForeColor="White" Height="38px" Width="164px" /></td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">
</asp:Content>

Please help!!! It is urgent...

sir good afternoon.plz help me, how randomly taking two questions from each five groups or catogory ?Total question should be ten.what is the code?

1. Load the questions into an array.

2. Call a random number generator (http://us2.php.net/rand) to select questions from the array.

3. As each question is selected, load the selected questions into a separate array.

4. Each time the random number generator is called, check the "selected questions" array to see if that question has already been used. If so, go back to Step 2 to select another question.

Repeat until the desired number of questions have been selected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.