自主原创、个人使用请注明出处:请勿用于商业用途,保留法律追责权利!
展示前台:
<fieldset>
<legend>查询列表</legend><div class="ListDiv">
<table class="table" id="tabList">
<thead>
<tr>
<th>
<input type="checkbox" id="chkHead" class="inputchk" />
</th>
<th style="text-align: left;">
姓名
</th>
<th style="text-align: left;">
学号
</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="rp_list" runat="server">
<ItemTemplate>
<tr>
<td style="text-align: center;">
<input type="checkbox" id="chkList" runat="server" />
</td>
<td>
<input type="hidden" runat="server" id="hidRowId" value='<%# Eval("rowid")%>' />
<%# Eval("student_name")%>
</td>
<td>
<%# Eval("study_no")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
<CService:TPager ID="AspNetPager1" runat="server" OnPagerChanged="PageChanged">
</CService:TPager>
</div>
</fieldset>
后台代码展示:
code
/// <summary>
/// 获取选中项的ID并保存在ViewState中
/// </summary>
public void GetSelectId()
{
Dictionary<string, string> dic = ViewState["rowid"] as Dictionary<string, string>;
if (ViewState["rowid"] == null)
dic = new Dictionary<string, string>();
if (rp_list.Items.Count == 0)
return;
else
{
string id = string.Empty;
if (dic == null)
dic = new Dictionary<string, string>();
for (int i = 0; i < rp_list.Items.Count; i++)
{
id = ((HtmlInputHidden)rp_list.Items[i].FindControl("hidRowId")).Value;
if (((HtmlInputCheckBox)rp_list.Items[i].FindControl("chkList")).Checked == true)
{
if (!dic.Keys.Contains(id))
dic.Add(id, id);
}
else
{
if (dic.Keys.Contains(id))
dic.Remove(id);
}
}
ViewState["rowid"] = dic;
}
}
/// <summary>
/// 恢复复选框选中状态
/// </summary>
public void ToRestoreTheSelectedItem()
{
string id = string.Empty;
Dictionary<string, string> dic = ViewState["rowid"] as Dictionary<string, string>;
for (int i = 0; i < rp_list.Items.Count; i++)
{
id = ((HtmlInputHidden)rp_list.Items[i].FindControl("hidRowId")).Value;
HtmlInputCheckBox chk = ((HtmlInputCheckBox)rp_list.Items[i].FindControl("chkList"));
if (dic.Keys.Contains(id))
chk.Checked = true;
}
}
在分页控件中的使用:
/// <summary>
/// 分页控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void PageChanged(object sender, EventArgs e)
{
GetSelectId();
BindData(false);
ToRestoreTheSelectedItem();
}