.Net GridView 应用:Gridview有一个CheckBox列,在用了分页的情况下,如何取出用户选择的所有记录id
2011-12-28 19:00
906 查看
思路一:每次换页时查询数据库,Session 保存已选
首先,分页保持每页先前选择的checkbox 的状态。
写了一个方法,遍历gridview,把选中的checkbox 的值存为arraylist 再保存 Session。在pageIndexChanging 事件里里调用。 (用;号分割,累加起来,样就遇到了一个问题,如果点了下一页,再回到上一页的话,Session 的值会有重复 )。
protected void gvFileList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues();
gvFileList.PageIndex = e.NewPageIndex;
bindData();
RePopulateValues();
}
/*保存分页前选择的值*/
private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
int index = -1;
foreach (GridViewRow row in gvFileList.Rows)
{
bool result = ((CheckBox)row.FindControl("chkfile")).Checked;
string str = returnStrRules(row);
// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!categoryIDList.Contains(str))
categoryIDList.Add(str);
}
else
categoryIDList.Remove(str);
}
if (categoryIDList != null && categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}
/* 取出分页前选择的值 初始选中状态 */
private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in gvFileList.Rows)
{
string str = returnStrRules(row);
if (categoryIDList.Contains(str))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("chkfile");
myCheckBox.Checked = true;
}
}
}
}
// 保存到 arraylist 的stri 的规则
private string returnStrRules(GridViewRow row)
{
string result = null;
result = gvFileList.DataKeys[row.RowIndex].Value.ToString ();
//string folio = row.Cells[2].Text.ToString();
//string processType = row.Cells[6].Text.ToString(); /* albert提醒在修改Sql查询语句后要记得修改processType的Cells属性索引*/
//bool result = ((CheckBox)row.FindControl("chkfile")).Checked;
//folio = index + ";" + folio + ";" + processType;
return result;
}
解析字符串
public string setReturnStr()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
string str = null;
foreach (object index in categoryIDList)
{
str += index.ToString() + ",";
}
str=str.Substring(0, str.Length - 1);
this.txtReturn.Value = str;
}
//Session["CHECKED_ITEMS"] = null;
return true.ToString();
}
js实现当前页全选(gridview->table)
function selectAll(obj)
{
var theTable = obj.parentElement.parentElement.parentElement;
var i;
var j = obj.parentElement.cellIndex;
for(i=0;i<theTable.rows.length;i++)
{
var objCheckBox = theTable.rows[i].cells[j].firstChild;
if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
}
}
<asp:GridView ID="gvFileList" runat="server" DataKeyNames="ID" AutoGenerateColumns="False"
Width="98%" AllowPaging="True" CssClass="tbl_border" OnPageIndexChanging="gvFileList_PageIndexChanging" PageSize="4">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkfile" />
</ItemTemplate>
<%-- <HeaderTemplate>
<input id="CheckAll" type="checkbox" onclick="selectAll(this);" />本页全选
</HeaderTemplate>--%>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="序号">
<ItemStyle Font-Size="Smaller" Width="10%" />
<HeaderStyle Font-Size="Smaller" />
</asp:BoundField>
</Columns>
<FooterStyle Height="2px" Width="2px" />
<PagerStyle Height="1px" Font-Size="Smaller" />
<SelectedRowStyle BorderColor="#0066CC" />
<HeaderStyle HorizontalAlign="Left" Width="2px" Height="2px" CssClass="tbl_borderTH" />
<AlternatingRowStyle CssClass="tbl_borderStripe" />
</asp:GridView>
首先,分页保持每页先前选择的checkbox 的状态。
写了一个方法,遍历gridview,把选中的checkbox 的值存为arraylist 再保存 Session。在pageIndexChanging 事件里里调用。 (用;号分割,累加起来,样就遇到了一个问题,如果点了下一页,再回到上一页的话,Session 的值会有重复 )。
protected void gvFileList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues();
gvFileList.PageIndex = e.NewPageIndex;
bindData();
RePopulateValues();
}
/*保存分页前选择的值*/
private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
int index = -1;
foreach (GridViewRow row in gvFileList.Rows)
{
bool result = ((CheckBox)row.FindControl("chkfile")).Checked;
string str = returnStrRules(row);
// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!categoryIDList.Contains(str))
categoryIDList.Add(str);
}
else
categoryIDList.Remove(str);
}
if (categoryIDList != null && categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}
/* 取出分页前选择的值 初始选中状态 */
private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in gvFileList.Rows)
{
string str = returnStrRules(row);
if (categoryIDList.Contains(str))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("chkfile");
myCheckBox.Checked = true;
}
}
}
}
// 保存到 arraylist 的stri 的规则
private string returnStrRules(GridViewRow row)
{
string result = null;
result = gvFileList.DataKeys[row.RowIndex].Value.ToString ();
//string folio = row.Cells[2].Text.ToString();
//string processType = row.Cells[6].Text.ToString(); /* albert提醒在修改Sql查询语句后要记得修改processType的Cells属性索引*/
//bool result = ((CheckBox)row.FindControl("chkfile")).Checked;
//folio = index + ";" + folio + ";" + processType;
return result;
}
解析字符串
public string setReturnStr()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
string str = null;
foreach (object index in categoryIDList)
{
str += index.ToString() + ",";
}
str=str.Substring(0, str.Length - 1);
this.txtReturn.Value = str;
}
//Session["CHECKED_ITEMS"] = null;
return true.ToString();
}
js实现当前页全选(gridview->table)
function selectAll(obj)
{
var theTable = obj.parentElement.parentElement.parentElement;
var i;
var j = obj.parentElement.cellIndex;
for(i=0;i<theTable.rows.length;i++)
{
var objCheckBox = theTable.rows[i].cells[j].firstChild;
if(objCheckBox.checked!=null)objCheckBox.checked = obj.checked;
}
}
<asp:GridView ID="gvFileList" runat="server" DataKeyNames="ID" AutoGenerateColumns="False"
Width="98%" AllowPaging="True" CssClass="tbl_border" OnPageIndexChanging="gvFileList_PageIndexChanging" PageSize="4">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkfile" />
</ItemTemplate>
<%-- <HeaderTemplate>
<input id="CheckAll" type="checkbox" onclick="selectAll(this);" />本页全选
</HeaderTemplate>--%>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="序号">
<ItemStyle Font-Size="Smaller" Width="10%" />
<HeaderStyle Font-Size="Smaller" />
</asp:BoundField>
</Columns>
<FooterStyle Height="2px" Width="2px" />
<PagerStyle Height="1px" Font-Size="Smaller" />
<SelectedRowStyle BorderColor="#0066CC" />
<HeaderStyle HorizontalAlign="Left" Width="2px" Height="2px" CssClass="tbl_borderTH" />
<AlternatingRowStyle CssClass="tbl_borderStripe" />
</asp:GridView>
相关文章推荐
- 我记录综合系统学习研究之用户管理五(如何利用wojilu打造一个全新的SNS应用)
- 如何在gridview中单击“选择”时,取出这条记录的值
- 在.NET客户端程序中应用多线程目录类与结构的实例比较类与结构的差别如何选择结构还是类一
- 同一个PC只能运行一个应用实例(考虑多个用户会话情况)
- 如何把所有的日志记录到同一个文件中???
- 只能选择GridView中的一个CheckBox(单选CheckBox)
- SQL Server中 如何取出一个表的所有列名
- GridView 中的checkbox加上选择所有的功能
- 一个查看所有用户的磁盘空间使用情况的脚本
- 如何查询一个用户下的所有表
- datagrid加checkbox实现分页不丢失选择的记录 (转)
- 简单实现:如何把所有的日志记录到同一个文件中???
- 数据库表中有重复记录,如何删除这些重复记录保留ID最大的一个信息
- Asp.net 实现GridView分页时记录CheckBox状态
- 重写的GridView,支持CheckBox选择,DataTable和List排序,SQL分页等,付源码
- LSTM模型在问答系统中的应用 2017-06-27 21:03 在问答系统的应用中,用户输入一个问题,系统需要根据问题去寻找最合适的答案。 1:采用句子相似度的方式。根据问题的字面相似度选择相似度最
- 如何用asp.net2.0的WSAT(网站管理工具)制作一个管理所有用户的网页
- 如:有个某市所有移动用户号码记录资料表需要更新,你将如何操作?
- datagrid加checkbox实现分页不丢失选择的记录 (转)