我正在使用ASP.NET制作考试Web应用程序。我正在使用网格视图来显示问题和可能的答案。我设置的网格视图大小为1,因此它只在页面索引上显示一个问题。我正在使用单选按钮进行多项选择。
当我选择单选按钮并转到网格视图的下一个问题或下一页索引并再次回到上一个问题时,我看到我之前选择的单选按钮未被选中。我该怎么做?
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1"
GridLines="None" Height="380px"
onpageindexchanging="GridView1_PageIndexChanging"
onprerender="GridView1_PreRender" onrowcommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged" PageSize="1"
ShowHeader="False" style="margin-right: 0px; margin-bottom: 0px;"
Width="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table style=" width:100%; height: 550px;">
<tr>
<td class="style6" colspan="2" colspan="3"
style="border-width: 1px; border-bottom-style: inset;">
<asp:Label ID="Label14" runat="server" Font-Size="Large" Text="Question No :-"></asp:Label>
<asp:Label ID="Label4" runat="server" Font-Bold="True" Font-Size="Large"
Text='<%# Eval("id") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style4" colspan="2" colspan="2">
<asp:Label ID="Label15" runat="server" Font-Bold="True" Text="Question:-"
Font-Size="Large"></asp:Label>
<br /> <asp:Label ID="Label6" runat="server" Font-Bold="False"
Text='<%# Eval("Question") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style9" style=" padding-top:10px">
A:-<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True"
Font-Bold="False"
oncheckedchanged="RadioButton1_CheckedChanged" Text='<%# Eval("A") %>' />
<br />
</td>
<td class="style1" rowspan="4" style=" padding-top:10px">
<asp:Image ID="Image1" runat="server" BorderStyle="None" Height="149px"
ImageUrl='<%# Eval("image") %>' Width="449px" />
</td>
</tr>
<tr>
<td class="style9">
B:-<asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True"
Font-Bold="False"
oncheckedchanged="RadioButton2_CheckedChanged" Text='<%# Eval("B") %>' />
<br />
</td>
</tr>
<tr>
<td class="style9">
C:-<asp:RadioButton ID="RadioButton3" runat="server" AutoPostBack="True"
Font-Bold="False"
oncheckedchanged="RadioButton3_CheckedChanged" Text='<%# Eval("C") %>' />
<br />
</td>
</tr>
<tr>
<td class="style9">
D:-<asp:RadioButton ID="RadioButton4" runat="server" AutoPostBack="True"
Font-Bold="False"
oncheckedchanged="RadioButton4_CheckedChanged" Text='<%# Eval("D") %>' />
<br />
</td>
</tr>
<tr>
<td colspan="2">
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<asp:Button ID="Button12" runat="server" BackColor="#006699" BorderStyle="None"
Font-Bold="True" Font-Size="Large" ForeColor="White" Height="34px"
Text="Next>>>" Width="230px" />
<asp:Button ID="Button2" runat="server" BackColor="#006699" BorderStyle="None"
CommandArgument='<%# Eval("id") %>' CommandName="Next" Font-Bold="True"
Font-Size="Large" ForeColor="White" Height="34px" Text="Mark for Review & Next"
Width="229px" />
<asp:Button ID="Button1" runat="server" BackColor="#006699" BorderStyle="None"
CommandArgument='<%# Eval("id") %>' CommandName="Save" Font-Bold="True"
Font-Size="Large" ForeColor="White" Height="34px" OnClick="Button1_Click"
Text="Save and Next" Width="230px" />
</td>
</tr>
</table>
</ItemTemplate>
<AlternatingItemTemplate>
<table style=" width:100%; height: 550px;">
<tr>
<td class="style9" colspan="2">
Q<span ID="GridView1_ctl02_Label14" style="font-size:Large;">0</span> .
<span ID="GridView1_ctl02_Label6" style="font-size:Large;font-weight:bold;">abc</span>
</td>
</tr>
<tr>
<td class="style9">
C:-<span style="font-size:Large;font-weight:normal;"><input
ID="GridView1_ctl02_RadioButton3" name="GridView1$ctl02$RadioButton3"
onclick="javascript:setTimeout('__doPostBack(\'GridView1$ctl02$RadioButton3\',\'\')', 0)"
type="radio" value="RadioButton3" /><label
for="GridView1_ctl02_RadioButton3">abc</label></span>
<br />
</td>
<td class="style6" colspan="2"
style=" border-width: 1px; border-bottom-style: inset;">
A:-<span style="font-size:Large;font-weight:bold;"
ID="GridView1_ctl02_Label4"><input
ID="GridView1_ctl02_RadioButton1" name="GridView1$ctl02$RadioButton1"
onclick="javascript:setTimeout('__doPostBack(\'GridView1$ctl02$RadioButton1\',\'\')', 0)"
type="radio" value="RadioButton1" /><label
for="GridView1_ctl02_RadioButton1">abc</label></span>
<br />
</td>
<td class="style1" rowspan="4" style=" padding-top:10px">
<img ID="GridView1_ctl02_Image1" Src=""
style="border-style:None;height:98px;width:231px;border-width:0px;" />
</td>
</tr>
<tr>
<td class="style4" colspan="2">
B:-<span style="font-weight:bold;" ID="GridView1_ctl02_Label15"><input
ID="GridView1_ctl02_RadioButton2" name="GridView1$ctl02$RadioButton2"
onclick="javascript:setTimeout('__doPostBack(\'GridView1$ctl02$RadioButton2\',\'\')', 0)"
type="radio" value="RadioButton2" /><label
for="GridView1_ctl02_RadioButton2">abc</label></span>
<br />
</td>
</tr>
<tr>
</tr>
<tr>
<td class="style9">
D:-<span style="font-size:Large;font-weight:normal;"><input
ID="GridView1_ctl02_RadioButton3" name="GridView1$ctl02$RadioButton3"
onclick="javascript:setTimeout('__doPostBack(\'GridView1$ctl02$RadioButton3\',\'\')', 0)"
type="radio" value="RadioButton3" /><label
for="GridView1_ctl02_RadioButton4">abc</label></span>
<br />
</td>
</tr>
<tr>
<td colspan="2">
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<input ID="GridView1_ctl02_Button12" name="GridView1$ctl02$Button12"
style="color:White;background-color:#006699;border-style:None;font-size:Large;font-weight:bold;height:34px;width:230px;"
type="submit" value="<<<Prv" /> <input
ID="GridView1_ctl02_Button2" name="GridView1$ctl02$Button2"
style="color:White;background-color:#006699;border-style:None;font-size:Large;font-weight:bold;height:34px;width:229px;"
type="submit" value="Mark and Next" />
<input ID="GridView1_ctl02_Button1" name="GridView1$ctl02$Button1"
style="color:White;background-color:#006699;border-style:None;font-size:Large;font-weight:bold;height:34px;width:230px;"
type="submit" value="Save and Next" />
</td>
</tr>
</table>
</AlternatingItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="White" BorderColor="White" ForeColor="White" />
</asp:GridView>
答案 0 :(得分:0)
使用代码在View-state中保存答案 if(e.CommandName ==“Save”) { foreach(GridView1.Rows中的GridViewRow行) {
if (((RadioButton)row.FindControl("RadioButton1")).Checked)
{
Label12.Text = "A";
}
if (((RadioButton)row.FindControl("RadioButton2")).Checked)
{
Label12.Text = "B";
}
if (((RadioButton)row.FindControl("RadioButton3")).Checked)
{
Label12.Text = "C";
}
if (((RadioButton)row.FindControl("RadioButton4")).Checked)
{
Label12.Text = "D";
}
string m = Convert.ToString(e.CommandArgument);
ViewState[m] = Label12.Text;
}
在GridView1的Row Data Bound事件中运行for循环 protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow)
{
string id = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "id"));
int i, j;
for (i = 1; i <= 65; i++)
{
j = i + 100;
string k = i.ToString();
string l = j.ToString();
if (ViewState[l] != null)
{
if (id == k)
{
if (ViewState[l].ToString() == "A")
{
(e.Row.Cells[0].FindControl("RadioButton1") as RadioButton).Checked = true;
}
if (ViewState[l].ToString() == "B")
{
(e.Row.Cells[0].FindControl("RadioButton2") as RadioButton).Checked = true;
}
if (ViewState[l].ToString() == "C")
{
(e.Row.Cells[0].FindControl("RadioButton3") as RadioButton).Checked = true;
}
if (ViewState[l].ToString() == "D")
{
(e.Row.Cells[0].FindControl("RadioButton4") as RadioButton).Checked = true;
}
}
}
}
}
}
解决!!!!!