B
Bodyboarder20
I'm currently using a GridView and during the RowDataBound event, I am
populated a radio list within the row. The problem is, when I click a
button on the bottom of the page, I am able to grab the
RadioButtonList from the GridView; however, it is not retaining the
users selection. It either retains -1 (no selection), or whatever I
set it to during the RowDataBound event.
Does anybody know how to get around this?
Thanks!!
Code:
------------------------------------------------------
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound"
runat="server" AutoGenerateColumns="false" AllowPaging="true"
DataSourceID="AccessDataSource1" DataKeyNames="ID" Width="100%"
PageSize="1" BorderStyle="None" BorderWidth="0px">
<Columns>
<asp:TemplateField>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table width="100%">
<tr>
<td><h3>Question <%#
GridView1.PageIndex+1 %>: <%# Eval("Question") %></h3></td>
</tr>
<tr>
<td>
<asp:RadioButtonList
ID="RBLAnswerList" runat="server" >
</asp:RadioButtonList>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Visible="False" />
</asp:GridView>
------------------------------------------------------
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
// Set session variables for this question
if (e.Row.RowType == DataControlRowType.DataRow)
{
Session.Add("CurrentQuestionID",
(int)DataBinder.Eval(e.Row.DataItem, "ID"));
Session.Add("CurrentQuestionCorrectAnswer",
(int)DataBinder.Eval(e.Row.DataItem, "CorrectAnswer"));
ArrayList answers =
FormatAnswers(DataBinder.Eval(e.Row.DataItem, "answers"));
RadioButtonList radio =
(RadioButtonList)e.Row.FindControl("RBLAnswerList");
radio.DataSource = answers;
radio.DataBind();
}
}
protected void NextButton_Click(object sender, EventArgs e)
{
RadioButtonList rbl =
(RadioButtonList)GridView1.Rows[0].FindControl("RBLAnswerList");
Answer ans = new Answer((int)Session["CurrentQuestionID"],
(int)Session["CurrentQuestionCorrectAnswer"],
rbl.SelectedIndex);
ArrayList al = (ArrayList)Session["QuizAnswerList"];
al.Add(ans);
Session.Add("QuizAnswerList", al);
if (GridView1.PageIndex == GridView1.PageCount - 1)
{
Response.Redirect("Results.aspx");
}
else
{
GridView1.PageIndex++;
}
if (GridView1.PageIndex == GridView1.PageCount - 1)
NextButton.Text = "Finished";
}
------------------------------------------------------
populated a radio list within the row. The problem is, when I click a
button on the bottom of the page, I am able to grab the
RadioButtonList from the GridView; however, it is not retaining the
users selection. It either retains -1 (no selection), or whatever I
set it to during the RowDataBound event.
Does anybody know how to get around this?
Thanks!!
Code:
------------------------------------------------------
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound"
runat="server" AutoGenerateColumns="false" AllowPaging="true"
DataSourceID="AccessDataSource1" DataKeyNames="ID" Width="100%"
PageSize="1" BorderStyle="None" BorderWidth="0px">
<Columns>
<asp:TemplateField>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table width="100%">
<tr>
<td><h3>Question <%#
GridView1.PageIndex+1 %>: <%# Eval("Question") %></h3></td>
</tr>
<tr>
<td>
<asp:RadioButtonList
ID="RBLAnswerList" runat="server" >
</asp:RadioButtonList>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Visible="False" />
</asp:GridView>
------------------------------------------------------
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
// Set session variables for this question
if (e.Row.RowType == DataControlRowType.DataRow)
{
Session.Add("CurrentQuestionID",
(int)DataBinder.Eval(e.Row.DataItem, "ID"));
Session.Add("CurrentQuestionCorrectAnswer",
(int)DataBinder.Eval(e.Row.DataItem, "CorrectAnswer"));
ArrayList answers =
FormatAnswers(DataBinder.Eval(e.Row.DataItem, "answers"));
RadioButtonList radio =
(RadioButtonList)e.Row.FindControl("RBLAnswerList");
radio.DataSource = answers;
radio.DataBind();
}
}
protected void NextButton_Click(object sender, EventArgs e)
{
RadioButtonList rbl =
(RadioButtonList)GridView1.Rows[0].FindControl("RBLAnswerList");
Answer ans = new Answer((int)Session["CurrentQuestionID"],
(int)Session["CurrentQuestionCorrectAnswer"],
rbl.SelectedIndex);
ArrayList al = (ArrayList)Session["QuizAnswerList"];
al.Add(ans);
Session.Add("QuizAnswerList", al);
if (GridView1.PageIndex == GridView1.PageCount - 1)
{
Response.Redirect("Results.aspx");
}
else
{
GridView1.PageIndex++;
}
if (GridView1.PageIndex == GridView1.PageCount - 1)
NextButton.Text = "Finished";
}
------------------------------------------------------