asp.net Gridview分页保存选项
#region //'revision: 1.00 created date: 2013/08/02 created id: una [#1300071]增加多選框
/// <summary>
/// session獲取多選框值
/// </summary>
private void rememberoldvalues()
{
arraylist categoryidlist = new arraylist();
string index = "";
foreach (gridviewrow row in gridview.rows)
{
index = (string)gridview.datakeys[row.rowindex].value;
bool result = ((checkbox)row.findcontrol("deletethis")).checked;
// check in the session
if (session["id"] != null)
categoryidlist = (arraylist)session["id"];
if (result)
{
if (!categoryidlist.contains(index))
categoryidlist.add(index);
}
else
categoryidlist.remove(index);
}
if (categoryidlist != null && categoryidlist.count > 0)
session["id"] = categoryidlist;
}
/// <summary>
/// session分頁時之前多選框為true
/// </summary>
private void repopulatevalues()
{
arraylist categoryidlist = (arraylist)session["id"];
if (categoryidlist != null && categoryidlist.count > 0)
{
foreach (gridviewrow row in gridview.rows)
{
string index = (string)gridview.datakeys[row.rowindex].value;
if (categoryidlist.contains(index))
{
checkbox mycheckbox = (checkbox)row.findcontrol("deletethis");
mycheckbox.checked = true;
}
}
}
}
#endregion
protected void gridview_pageindexchanging(object sender, gridviewpageeventargs e)
{
rememberoldvalues();
gridview.pageindex = e.newpageindex;
binddata();
repopulatevalues();
}
protected void btnselect_click(object sender, eventargs e)
{
string items = "";
arraylist categoryidlist = new arraylist();
string index ="";
foreach (gridviewrow row in gridview.rows)
{
index = (string)gridview.datakeys[row.rowindex].value;
bool result = ((checkbox)row.findcontrol("deletethis")).checked;
// check in the session
if (session["id"] != null)
categoryidlist = (arraylist)session["id"];
if (result)
{
if (!categoryidlist.contains(index))
categoryidlist.add(index);
}
else
categoryidlist.remove(index);
}
if (categoryidlist != null && categoryidlist.count > 0)
for (int i = 0; i < categoryidlist.count; i++)
{
items += categoryidlist[i] + ",";
}
items = items.substring(0, items.length - 1);
scriptmanager.registerstartupscript(this, this.gettype(), "", "check('" + items + "');", true);
session.remove("id");
}
上一篇: 基于.Net中的协变与逆变的深入分析
下一篇: 完整的Android表情功能处理方案