|
I m making an online examination page on this page i created a gridview with paging when i go on next page thn aftr selecting options of answer of any question i m going on to another page. but when i come back on this previous page i cant see the previously selected answers the gridview page is refreshed thn, Please help me in it.
<body>
<div id="timer" style="float:right;">
<label id="tim" style="font-size:15px;"></label>
</div>
<div>
</body>
<asp:ScriptManager ID="scrpt1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updt" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="examgrid" runat="server" AllowPaging="true"
AutoGenerateColumns="false" onrowdatabound="examgrid_RowDataBound"
PageSize="2" onpageindexchanging="examgrid_PageIndexChanging"
PageIndex="10" Width="528px" EnableViewState="false"
onrowcommand="examgrid_RowCommand"
onpageindexchanged="examgrid_PageIndexChanged" onload="examgrid_Load">
<Columns>
<asp:TemplateField HeaderText="Question">
<ItemTemplate>
<asp:Label ID="ques" runat="server" Text='<%#Eval("Ques_Name") %>'></asp:Label>
<asp:HiddenField ID="Question_Id" runat="server" Value='<%#Eval("Ques_Id") %>' /><br /><br />
<asp:RadioButton ID="opt1" runat="server" GroupName="Radio" Text='<%#Eval("Option1") %>'/>
<br />
<asp:RadioButton ID="opt2" runat="server" GroupName="Radio" Text='<%#Eval("Option2") %>'/>
<br />
<asp:RadioButton ID="opt3" runat="server" GroupName="Radio" Text='<%#Eval("Option3") %>' />
<br />
<asp:RadioButton ID="opt4" runat="server" GroupName="Radio" Text='<%#Eval("Option4") %>'/>
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:HiddenField ID="hidvalue" runat="server" EnableViewState="true" />
<asp:Button ID="Submitbtn" runat="server" Text="Submit" style="float:right;"
onclick="Submitbtn_Click"/>
</div>
and here is my c# code in gridview paging
protected void examgrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
DBClass db = new DBClass();
for (int i = 0; i < examgrid.Rows.Count; i++)
{
Label question = (Label)examgrid.Rows[i].Cells[0].FindControl("ques");
HiddenField quesid = (HiddenField)examgrid.Rows[i].Cells[0].FindControl("Question_Id");
RadioButton rd1 = (RadioButton)examgrid.Rows[i].Cells[0].FindControl("opt1");
RadioButton rd2 = (RadioButton)examgrid.Rows[i].Cells[0].FindControl("opt2");
RadioButton rd3 = (RadioButton)examgrid.Rows[i].Cells[0].FindControl("opt3");
RadioButton rd4 = (RadioButton)examgrid.Rows[i].Cells[0].FindControl("opt4");
string user = "Arun";
if (rd1.Checked == true || rd2.Checked == true || rd3.Checked == true || rd4.Checked == true)
{
if (rd1.Checked == true)
{
db.InsAnswer(user, question.Text, rd1.Text);
Session["selectedOption_"+quesid+"_Answer"]=rd1.Text;
}
else
if (rd2.Checked == true)
{
db.InsAnswer(user, question.Text, rd2.Text);
Session["selectedOption_"+quesid+"_Answer"]=rd2.Text;
}
else if (rd3.Checked == true)
{
db.InsAnswer(user, question.Text, rd3.Text);
Session["selectedOption_"+quesid+"_Answer"]=rd3.Text;
}
else if (rd4.Checked == true)
{
db.InsAnswer(user, question.Text, rd4.Text);
Session["selectedOption_"+quesid+"_Answer"]=rd4.Text;
}
}
else
{
for (int j = 0; j < examgrid.Rows.Count; j++)
{
Label ques = (Label)examgrid.Rows[j].Cells[0].FindControl("ques");
int y = db.select_QuestionId(ques.Text);
string selected_option = db.Select_Ansqwer(question.Text, user);
if (Session["selectedOption_"+y+"_Answer"]==selected_option )
{
if (selected_option == rd1.Text)
{
rd1.Checked = true;
}
else if (selected_option == rd2.Text)
{
rd2.Checked = true;
}
else if (selected_option == rd3.Text)
{
rd3.Checked = true;
}
else if (selected_option == rd4.Text)
{
rd4.Checked = true;
}
}
}
}
}
}
|
|
|
|
|
Generally the implementation is to rebind the grid on page change that leads to loss of any selection made.
If you want to persist selection across various pages, you need to maintain the selection somewhere or use client side pagination avoiding rebind on page change. Following article demonstrates one of the ways to handle it: Maintaining States of Selected CheckBoxes in Different Pages inside the GridView[^]
|
|
|
|
|
If i have to give an opinion,i'll say that use javascript to store the value corresponding to the question no and show that everytime. By default keep nothing in the radiobutton.
Hope this helps.
|
|
|
|