RadioButtonList problem...

  • Thread starter Thread starter Bodyboarder20
  • Start date Start date
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";
}
------------------------------------------------------
 
I found my problem!

I was databinding to the GridView at the bottom my PageLoad. This was
getting called before my Button click event... hence overwriting the
users selection back to default.
 
Thank you...
Even I was facing the same problem and tat was also due to similar binding I had done.......
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";
}
------------------------------------------------------
On Tuesday, September 25, 2007 12:43 PM Bodyboarder20 wrote:
I found my problem!

I was databinding to the GridView at the bottom my PageLoad. This was
getting called before my Button click event... hence overwriting the
users selection back to default.
 
Back
Top