62,204
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" Debug="false" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
String Articles = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["ArticleId"]!=null) Articles = Request.Cookies["ArticleId"].Value;
Articles = Server.UrlDecode(Articles);
string xlsConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|MengxianHui.mdb";
OleDbConnection cn = new OleDbConnection(xlsConnStr);
cn.Open();
String sql = "SELECT Count(*) FROM [Document]";
OleDbCommand cmd = new OleDbCommand(sql, cn);
// 总的记录数
int TotalCount = Convert.ToInt32(cmd.ExecuteScalar());
//当前页的序号
int PageIndex = 1;
String page = Request.QueryString["Page"];
if (page == null) page = "1";
if (!Int32.TryParse(page, out PageIndex)) PageIndex = 1;
if (PageIndex < 1) PageIndex = 1;
//每页显示的数量
int PageItem = 5;
int startRecord = (PageIndex - 1) * PageItem;
sql = "SELECT [ArticleId],[Title],[CreateDate],[Author] FROM [Document] Order By [ArticleId] DESC";
OleDbDataAdapter da = new OleDbDataAdapter(sql, cn);
DataSet ds = new DataSet();
da.Fill(ds, startRecord, PageItem, "Document");
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
BuildPagers(TotalCount, PageIndex, PageItem);
cn.Dispose();
}
private void BuildPagers(int TotalCountRecord, int CurrentPage, int PageItem)
{
int Step = 6;
int LeftNum = 0;
int RightNum = 0;
String PageUrl = "?";
int PageCount = (int)Math.Ceiling((double)(TotalCountRecord) / PageItem);
if (CurrentPage - Step < 1)
{
LeftNum = 1;
}
else
{
LeftNum = CurrentPage - Step;
}
if (CurrentPage + Step > PageCount)
{
RightNum = PageCount;
}
else
{
RightNum = CurrentPage + Step;
}
string OutPut = "";
for (int i = LeftNum; i <= RightNum; i++)
{
if (i == CurrentPage)
{
OutPut += "<span style='color:red'>" + i.ToString() + "</span> ";
}
else
{
OutPut += "<a href=\"" + PageUrl + "Page=" + i.ToString() + "\">" + i.ToString() + "</a> ";
}
}
if (CurrentPage > 1)
{
OutPut = "<a href='" + PageUrl + "Page=1'>首页</a> <a href=\"" + PageUrl + "Page=" + (CurrentPage - 1) + "\">上一页</a> " + OutPut;
}
if (CurrentPage < PageCount)
{
OutPut += " <a href=\"" + PageUrl + "Page=" + (CurrentPage + 1) + "\">下一页</a> <a href='" + PageUrl + "Page=" + PageCount + "'>末页</a>";
}
Pager.InnerHtml = OutPut;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String CurrentArticleId = DataBinder.Eval(e.Row.DataItem, "ArticleId").ToString();
CheckBox ArticleId = e.Row.FindControl("ArticleId") as CheckBox;
ArticleId.Attributes.Add("onclick", "SetArticleId(this," + CurrentArticleId + ")");
if (Articles.IndexOf("|" + CurrentArticleId + "|") > -1)
{
ArticleId.Checked = true;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String[] ids = Articles.Split('|');
for (int i = 0; i < ids.Length; i++)
{
if (!ids[i].Trim().Equals(String.Empty))
{
Response.Write("<li>" + ids[i]);
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>GET 方式实现 GridView 跨页面多选</title>
<script type="text/javascript">
function SetArticleId(o, i) {
if (o.checked) {
AddCookie(i)
}
else {
RemoveCookie(i)
}
}
function SetCookie(name, value) {
document.cookie = name + "=" + escape(value);
}
function GetCookie(name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(name + "=");
if (c_start != -1) {
c_start = c_start + name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
function AddCookie(i) {
d = GetCookie("ArticleId");
if (d == "") d = "|";
if (d.indexOf("|" + i + "|") == -1) {
d += i + "|";
SetCookie("ArticleId", d);
}
}
function RemoveCookie(i) {
d = GetCookie("ArticleId");
var reg = new RegExp("\\|" + i + "\\|");
if (reg.test(d)) {
d = d.replace(reg, "|");
SetCookie("ArticleId", d);
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" CellPadding="6"
ShowHeader="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ArticleId" runat="server" /></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div id="Pager" runat="server"></div>
<asp:Button ID="Button1" runat="server" Text="查看所选择的ID"
onclick="Button1_Click" />
</form>
</body>
</html>