思路一:每次换页时查询数据库,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>