| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 909 人关注过本帖
标题:gridview分页保留模板内RadioButtonList保留选中状态问题
只看楼主 加入收藏
zxd543
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:内蒙古
等 级:贵宾
威 望:17
帖 子:453
专家分:2351
注 册:2012-4-12
结帖率:100%
收藏
已结贴  问题点数:50 回复次数:2 
gridview分页保留模板内RadioButtonList保留选中状态问题
前台就是每一页只显示一道选择题信息。。。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StudentStartExam11.aspx.cs" Inherits="Student_StudentStartExam11" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. <html xmlns="http://www.
<head runat="server">
<title>无标题页</title>
<style type="text/css">
#form1
{
height: 613px;
}
.style35
{
width: 103%;
height: 20px;
}
.style67
{
height: 19px;
}
.style68
{
height: 18px;
}
.style60
{
width: 36px;
}
.style63
{
width: 37px;
}
.style66
{
height: 23px;
}
.style56
{
height: 24px;
width: 36px;
}
.style53
{
height: 24px;
}
.style64
{
height: 31px;
width: 36px;
}
.style65
{
height: 31px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<br />
<br />
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate >
<asp:GridView ID="gvDanXuan" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" Font-Size="10pt"
ForeColor="#333333" GridLines="None" Height="188px"
onpageindexchanging="gvDanXuan_PageIndexChanging" PageSize="1"
Width="659px">
<PagerSettings FirstPageText="首页" LastPageText="尾页"
Mode="NextPreviousFirstLast" NextPageText="下一页"
PreviousPageText="上一页" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:TemplateField HeaderText="选择题(每题2分)">
<ItemTemplate>
<table class="style35">
<tr>
<td class="style67">
<asp:Label ID="lab_dxId" runat="server" Text='<%# Eval("dx_Id") %>'></asp:Label>
</td>
<td class="style68" colspan="2">
<asp:Label ID="labContext" runat="server" Text='<%# Eval("dx_Context") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style60">
&nbsp;</td>
<td class="style63" rowspan="4">
<asp:RadioButtonList ID="rbtDanXuan" runat="server" Height="16px"
Width="16px" AutoPostBack="True">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>D</asp:ListItem>
</asp:RadioButtonList>
</td>
<td align="left" class="style66">
<br />
<asp:Label ID="labAnswerA" runat="server" Text='<%# Eval("dx_AnswerA") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style56">
</td>
<td class="style53">
<asp:Label ID="labAnswerB" runat="server" Text='<%# Eval("dx_AnswerB") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style56">
&nbsp;</td>
<td class="style53">
<asp:Label ID="labAnswerC" runat="server" Text='<%# Eval("dx_AnswerC") %>'></asp:Label>
</td>
</tr>
<tr>
<td class="style64">
</td>
<td class="style65">
<asp:Label ID="labAnswerD" runat="server" Text='<%# Eval("dx_AnswerD") %>'></asp:Label>
<br />
<br />
</td>
</tr>
</table>
</ItemTemplate>
<ItemStyle Font-Size="10pt" ForeColor="Black" HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

</form>
</body>
</html>


后台
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; public partial class Student_StudentStartExam11 : System.Web.UI.Page
{

//保存RadioButtonList选择的项
protected void Save()
{
Hashtable ht = new Hashtable(); //实例化一个Hashtable,用于储存RadioButtonList选中的值。
for (int i = 0; i < this.gvDanXuan.Rows.Count; i++) //循环GridView每一行
{
int id = Convert.ToInt32(this.gvDanXuan.DataKeys[i].Value.ToString()); //获取GridView每一行对应的id
RadioButtonList rbtn = (RadioButtonList)this.gvDanXuan.Rows[i].FindControl("rbtDanXuan");
int matter = Convert.ToInt32(rbtn.SelectedValue);
if (ViewState["ht"] != null)
{
ht = (Hashtable)ViewState["ht"];
if (ht.Contains(id))
{
ht.Remove(id);
}
}
ht.Add(id, matter);
ViewState["ht"] = ht;
}
}
//还原RadioButtonList选择的项
protected void Revert()
{
Hashtable ht = (Hashtable)ViewState["ht"];
for (int i = 1; i < this.gvDanXuan.Rows.Count; i++)
{
int id = Convert.ToInt32(this.gvDanXuan.DataKeys[i].Value.ToString());
if (ht.Contains(id))
{
int matter = (int)ht[id];
RadioButtonList rbtn = (RadioButtonList)this.gvDanXuan.Rows[i].FindControl("rbtDanXuan");
rbtn.SelectedIndex = matter;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strDanXuan = "select * from course_DanXuan where TeaName = '" + Session["teacher"].ToString() + "'";
CExamDatabase.BindGridView1(gvDanXuan, strDanXuan);
//CExamDatabase.BindGridView1(GridView1, strDanXuan);

}
}
protected void gvDanXuan_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Save();
this.gvDanXuan.PageIndex = e.NewPageIndex;
string strDanXuan = "select * from course_DanXuan where TeaName = '" + Session["teacher"].ToString() + "'";
CExamDatabase.BindGridView1(gvDanXuan, strDanXuan);
Revert();
}

问题1;运行后分页不可用this.gvDanXuan.Rows.Count的值为1;不进入循环,求解决方法,不想直接存数数据库!
搜索更多相关主题的帖子: html server 选择题 PUBLIC style 
2013-03-04 00:12
elongtown
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:12
帖 子:138
专家分:1003
注 册:2013-2-18
收藏
得分:25 
帮顶,高手来
2013-03-04 06:31
party620
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:696
专家分:2521
注 册:2013-1-31
收藏
得分:25 
为什么不用Dictionary<key,value>来存储RadioButtonList选中的值?
2013-03-04 17:00
快速回复:gridview分页保留模板内RadioButtonList保留选中状态问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016324 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved