I have nested gridviews, in the outer grid I am displaying the question and in the inner grid 4 options are displayed in radiobuttons. I have kept Lock Answer button inside Item Template in inner gridview , so this button is coming with each and every question and when i click on Lock Answer button in front of any question, its saving the corresponding answer in the database. For ex, if there are 10 questions this button is coming with all the questions, all the questions are on single page and all are multiple choice. Now everything is working mine, my requirement is that I need on 1 common Lock Answer button at the bottom of the page and not with all the questions. And when I click on that common button it should run for all the questions that are displayed on the screen at should fetch and update each unique answer corresponding to that question in the database. Below is my code: Kindly suggest solution.
<asp:GridView ID="gvouter" runat="server" GridLines="None" AutoGenerateColumns="False"
AllowPaging="false" OnRowDataBound="gvouter_RowDataBound" Width="100%" OnRowCommand="gvouter_RowCommand"
OnPageIndexChanging="gvouter_PageIndexChanging" >
<PagerSettings Position="Top" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="">
<div class="">
<div class="question" id="grid1">
<h3 class="color_bluedark"> Qs.
<%# Container.DataItemIndex + 1 %><asp:Label ID="lbl_findit" runat="server"></asp:Label></h3>
<div class="span6">
<div class="btn_next_skip">
</div>
</div>
<p><asp:Label ID="lblq" runat="server" Text='<%#Eval("Question") %>'></asp:Label></p>
</div>
<asp:Label ID="lblimg" runat="server" Text='<%#Eval("Image") %>' Visible="false"></asp:Label>
<asp:Image ID="imgquest" runat="server" />
<asp:Label ID="lblsubid" runat="server" Text='<%#Eval("SubjectID") %>' Visible="false"></asp:Label>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("QID") %>' Visible="false"></asp:Label>
<asp:GridView ID="gvinner" CssClass="" OnRowCommand="gvinner_RowCommand" runat="server"
OnRowDataBound="gvinner_RowDataBound" AutoGenerateColumns="false" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="answer" runat="server">
<small class="text-muted">Select your answer from the below options</small>
<asp:Label ID="Label5" runat="server" Text='<%#Eval("innerqtext") %>'></asp:Label>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("QID") %>' Visible="false"></asp:Label>
<asp:Label ID="lblinnerid" runat="server" Text='<%#Eval("id") %>' Visible="false"></asp:Label>
<asp:Label ID="lblchkuseranswer" runat="server" Text='<%#Eval("useranswer") %>' Visible="false"></asp:Label>
<asp:Label ID="lblsubid" runat="server" Text='<%#Eval("subid") %>' Visible="false"></asp:Label>
<asp:Image ID="imgopt1" runat="server" Visible="false" ImageUrl='<%#"QuestionsImages/"+Eval("Image1")%>' />
<div class="radio">
<asp:RadioButton ID="radiobutton1" GroupName="a" Text='<%#"<strong>A. </strong>"+ Eval("opt1") %>'
runat="server"/>
<asp:Image ID="imgopt2" runat="server" Visible="false" ImageUrl='<%#"QuestiosnImages/"+Eval("Image2")%>' />
</div>
<div class="radio">
<asp:RadioButton ID="radiobutton2" GroupName="a" Text='<%# "<strong>B. </strong>"+Eval("opt2") %>'
runat="server" />
<asp:Image ID="imgopt3" runat="server" Visible="false" ImageUrl='<%#"QuestiosnImages/"+Eval("Image3")%>' />
</div>
<div class="radio">
<asp:RadioButton ID="radiobutton3" GroupName="a" Text='<%# "<strong>C. </strong>"+Eval("opt3") %>'
runat="server" />
<asp:Image ID="imgopt4" runat="server" Visible="false" ImageUrl='<%#"QuestionsImages/"+Eval("Image4")%>' />
</div>
<div class="radio">
<asp:RadioButton ID="radiobutton4" GroupName="a" Text='<%# "<strong>D. </strong>"+Eval("opt4") %>'
runat="server" />
<asp:Label ID="lblcorrect" runat="server" Text='<%#Eval("correctanswer") %>' Visible="false"></asp:Label>
<br />
<br />
</div>
</div>
</div></div>
<div class="row-fluid">
<div class="btn_next1" id="btnouter" runat="server">
<div class="span12">
<asp:LinkButton ID="btnsubmit" runat="server" Text='Lock ' CssClass="btn btn-info btn-sm"
CommandName="lock" CommandArgument='<%# Eval("id")+ ";"+ Eval("QID") + ";"+ Eval("subid")%>'>
</asp:LinkButton>
</div>
</div>
<div class="btn_next" id="btninnerpassage" runat="server">
<div class="span12">
<asp:LinkButton ID="lnkpassagenext" runat="server"
Text="<i class='icon-chevron-right icon-white'></i> Goto next question"
Visible="false" CssClass="btn btn-success" CommandName="next">
</asp:LinkButton>
</div>
</div>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="btn" runat="server" Text="Submit" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gvinner_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "lock")
{
string[] arg = new string[3];//string array of length 3 to store 3 command arguments .
arg = e.CommandArgument.ToString().Split(';');
dt = new DataTable();
dt = tb;
int count_rows_for_saving_answers = gvouter.Rows.Count;
for (int ii=0;ii<=count_rows_for_saving_answers-1;ii++)
{
GridView gvinner = (GridView)gvouter.Rows[ii].FindControl("gvinner");
id = (Label)gvouter.Rows[ii].FindControl("lblid");
Label lblsubid = (Label)gvouter.Rows[ii].FindControl("lblsubid");
subjectid = lblsubid.Text.ToString();
chkpassagerows = 0;
foreach (GridViewRow dr in gvinner.Rows)
{
int a = dr.RowIndex;
chkpassagerows = gvinner.Rows.Count;
correct = (Label)(gvinner.Rows[dr.RowIndex].FindControl("lblcorrect"));
opt1 = (RadioButton)(gvinner.Rows[dr.RowIndex].FindControl("radiobutton1"));
opt2 = (RadioButton)(gvinner.Rows[dr.RowIndex].FindControl("radiobutton2"));
opt3 = (RadioButton)(gvinner.Rows[dr.RowIndex].FindControl("radiobutton3"));
opt4 = (RadioButton)(gvinner.Rows[dr.RowIndex].FindControl("radiobutton4"));
Label lblinnerid = (Label)(gvinner.Rows[dr.RowIndex].FindControl("lblinnerid"));
LinkButton btnsubmit = (LinkButton)(gvinner.Rows[dr.RowIndex].FindControl("btnsubmit"));
if (opt1.Checked == true)
{
option = "a";
}
if (opt2.Checked == true)
{
option = "b";
}
if (opt3.Checked == true)
{
option = "c";
}
if (opt4.Checked == true)
{
option = "d";
}
string mid = arg[0].ToString();
if (mid == lblinnerid.Text)
{
string qid = arg[1].ToString();
string sid = arg[2].ToString();
SqlParameter[] sqlParams = new SqlParameter[4];
sqlParams[0] = new SqlParameter("@id", mid);
sqlParams[1] = new SqlParameter("@qid", qid);
sqlParams[2] = new SqlParameter("@subid", sid);
sqlParams[3] = new SqlParameter("@useranswer", option);
SqlHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "updateExamuseranswers", sqlParams);
}
else
{
}
}
}
}
}
catch { }
}