读书人

是高手的就解决GridView自定分页有关问

发布时间: 2012-01-05 22:36:54 作者: rapoo

是高手的就解决GridView自定分页问题(包含页码)
用 <PagerTemplate> </PagerTemplate> 进行自定义分页功能,
要求有:

第N页/共M页 首页 下一页 1 2 3 4 5 6 7 8 9 10 ... 上一页 未页 转到 X 页


不知哪个高手能解决这样的问题?

[解决办法]
来看高手 up
[解决办法]
在gridView自带的分页功能里
PagerSettings有四个mode
可惜的是都没有达到你要求的那种效果
这个你要自己写代码了
网上关于分页的蛮多的
找找看吧
[解决办法]
自定义分页
[解决办法]
做过datagrid的
可以参考一下

<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "datagrid.aspx.cs " Inherits= "dataset_datagrid " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:DataGrid ID= "DataGrid1 " OnDeleteCommand= "DataGrid1_DeleteCommand " OnUpdateCommand= "DataGrid1_UpdateCommand " OnCancelCommand= "DataGrid1_CancelCommand " OnEditCommand= "DataGrid1_EditCommand " runat= "server " AutoGenerateColumns= "False " CellPadding= "4 " BackColor= "White " BorderColor= "#3366CC " BorderWidth= "1px " Width= "747px " AllowPaging= "True " OnPageIndexChanged= "DataGrid1_PageIndexChanged " OnItemCreated= "DataGrid1_ItemCreated " PageSize= "5 " BorderStyle= "None " >
<Columns>
<asp:EditCommandColumn CancelText= "取消 " EditText= "编辑 " UpdateText= "更新 " HeaderText= "编辑 "> </asp:EditCommandColumn>
<asp:BoundColumn DataField= "用户名 " HeaderText= "用户名 " ReadOnly= "True "> </asp:BoundColumn>
<asp:BoundColumn DataField= "密码 " HeaderText= "密码 "> </asp:BoundColumn>
<asp:BoundColumn DataField= "昵称 " HeaderText= "昵称 "> </asp:BoundColumn>
<asp:BoundColumn DataField= "电子邮件 " HeaderText= "电子邮件 "> </asp:BoundColumn>
<asp:BoundColumn DataField= "专业 " HeaderText= "专业 "> </asp:BoundColumn>
<asp:ButtonColumn CommandName= "Delete " Text= "删除 " HeaderText= "删除 "> </asp:ButtonColumn>
<asp:HyperLinkColumn DataNavigateUrlField= "专业 " DataNavigateUrlFormatString= "http://localhost/1.asp?proid={0} "
DataTextField= "专业 " HeaderText= "连接 " Target= "_blank "> </asp:HyperLinkColumn>
</Columns>
<FooterStyle BackColor= "#99CCCC " ForeColor= "#003399 " />
<SelectedItemStyle BackColor= "#009999 " ForeColor= "#CCFF99 " Font-Bold= "True " />
<PagerStyle BackColor= "#99CCCC " ForeColor= "#003399 " HorizontalAlign= "Center " Mode= "NumericPages " />


<HeaderStyle BackColor= "#003399 " Font-Bold= "True " ForeColor= "#CCCCFF " />
<ItemStyle BackColor= "White " ForeColor= "#003399 " />
</asp:DataGrid> </div>
</form>
</body>
</html>

[解决办法]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class dataset_datagrid : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
}
}
protected void LoadData()
{
string sconn = "uid=sa;pwd=;database=tu;server= ";
SqlConnection conn = new SqlConnection(sconn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter( "select * from userinfo ", conn);
DataSet ds = new DataSet();
da.TableMappings.Add( "Table ", "userinfo ");
da.TableMappings[0].ColumnMappings.Add( "username ", "用户名 ");
da.TableMappings[0].ColumnMappings.Add( "password ", "密码 ");
da.TableMappings[0].ColumnMappings.Add( "nickname ", "昵称 ");
da.TableMappings[0].ColumnMappings.Add( "email ", "电子邮件 ");
da.TableMappings[0].ColumnMappings.Add( "major ", "专业 ");
da.Fill(ds);
DataGrid1.DataSource = ds.Tables[ "userinfo "].DefaultView;
ViewState[ "Row "] = ds.Tables[ "userinfo "].DefaultView.Count;
DataGrid1.DataBind();
}
public void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
LoadData();
}
public void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
string sconn = "uid=sa;pwd=;database=tu;server= ";
string query = "update userinfo set nickname=@nickname,password=@password,email=@email,major=@major where username= ' " + e.Item.Cells[1].Text + " ' ";
SqlConnection conn = new SqlConnection(sconn);
SqlCommand cmd = new SqlCommand(query, conn);
SqlParameter sp2 = new SqlParameter( "@password ", SqlDbType.Char, 20);
SqlParameter sp3 = new SqlParameter( "@nickname ", SqlDbType.Char, 20);
SqlParameter sp4 = new SqlParameter( "@email ", SqlDbType.Char, 50);
SqlParameter sp5 = new SqlParameter( "@major ", SqlDbType.Char, 10);
cmd.Parameters.Add(sp2);
cmd.Parameters.Add(sp3);
cmd.Parameters.Add(sp4);
cmd.Parameters.Add(sp5);
sp2.Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
sp3.Value = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
sp4.Value = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
sp5.Value = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
DataGrid1.EditItemIndex = -1;
LoadData();
}
public void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)


{
DataGrid1.EditItemIndex = -1;
LoadData();
}
public void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string sconn = "uid=sa;pwd=;database=tu;server= ";
SqlConnection conn = new SqlConnection(sconn);
string a1;
a1 = e.Item.Cells[1].Text;
string query = "delete from userinfo where username= ' " + a1 + " ' ";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
cmd.Dispose();
LoadData();
}

[解决办法]
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
LoadData();
}
protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
ListItemType elemType = e.Item.ItemType;
if (elemType == ListItemType.Pager)
{
TableCell pager = (TableCell)e.Item.Controls[0];
int up = 0;
int down = 0;
for (int i = 0; i < pager.Controls.Count; i += 2)
{
Object o = pager.Controls[i];
if (o is LinkButton)
{
LinkButton h = (LinkButton)o;
if (h.Text != "... ")
{
h.ToolTip = "跳转到第 " + h.Text + "页 ";
}
if (i == 2)
{
up = int.Parse(h.Text) - 1;
}
if (i == pager.Controls.Count - 3)
{
down = int.Parse(h.Text) + 1;
}
}
else
{
Label l = (Label)o;

if (i == 2)
{
up = int.Parse(l.Text) - 1;
}
if (i == pager.Controls.Count - 3)
{
down = int.Parse(l.Text) + 1;
}

l.Text = "[ " + l.Text + "] ";
l.ForeColor = System.Drawing.Color.Red;
}
}
Object oo = pager.Controls[0];
if (oo is LinkButton)
{
LinkButton h = (LinkButton)oo;
if (h.Text == "... ")
{
h.Text = " ";
}
}
Object ooo = pager.Controls[pager.Controls.Count - 1];
if (ooo is LinkButton)
{
LinkButton h = (LinkButton)ooo;
if (h.Text == "... ")
{
h.Text = " ";
}
}
Label la = new Label();
la.Text = " <table width=100%> <tr> <td align=left> 总记录数: <b> " + ViewState[ "Row "].ToString() + " </b> ";
la.Text += "总页数: <b> " + this.DataGrid1.PageCount + " </b> ";
la.Text += "当前页: <font color=red> <b> " + (this.DataGrid1.CurrentPageIndex + 1).ToString() + " </b> </font> </td> <td align=right> ";


pager.Controls.AddAt(0, la);

ImageButton IB1 = new ImageButton();
IB1.ImageUrl = "~/image/backtotop.jpg ";
IB1.ToolTip = "跳转到首页 ";
IB1.Click += new System.Web.UI.ImageClickEventHandler(this.IB1_Click);
pager.Controls.AddAt(1, IB1);

Label lo = new Label();
lo.Text = "   ";
pager.Controls.AddAt(2, lo);
if (this.DataGrid1.CurrentPageIndex > 0)
{
ImageButton IB2 = new ImageButton();
IB2.ImageUrl = "~/image/back.jpg ";
IB2.ToolTip = "上一页 ";
IB2.Click += new System.Web.UI.ImageClickEventHandler(this.IB2_Click);
pager.Controls.AddAt(3, IB2);

Label l1 = new Label();
l1.Text = "   ";
pager.Controls.AddAt(4, l1);
}

if (this.DataGrid1.CurrentPageIndex < this.DataGrid1.PageCount - 1)
{
Label l2 = new Label();
l2.Text = "   ";
pager.Controls.Add(l2);

ImageButton IB3 = new ImageButton();
IB3.ImageUrl = "~/image/go.jpg ";
IB3.ToolTip = "下一页 ";
IB3.Click += new System.Web.UI.ImageClickEventHandler(this.IB3_Click);
pager.Controls.Add(IB3);
}


Label l3 = new Label();
l3.Text = "   ";
pager.Controls.Add(l3);

ImageButton IB4 = new ImageButton();
IB4.ImageUrl = "~/image/gototop.jpg ";
IB4.ToolTip = "跳转到尾页 ";
IB4.Click += new System.Web.UI.ImageClickEventHandler(this.IB4_Click);
pager.Controls.Add(IB4);

Label l4 = new Label();
l4.Text = "  转到第 ";
pager.Controls.Add(l4);

TextBox tb = new TextBox();
tb.Width = 30;
tb.ID = "tb ";
pager.Controls.Add(tb);

Label l5 = new Label();
l5.Text = "页 ";
pager.Controls.Add(l5);

Button bt = new Button();
bt.Text = "跳转 ";
bt.Click += new System.EventHandler(this.bt_Click);
pager.Controls.Add(bt);

Label lb = new Label();
lb.Text = " </td> </tr> </table> ";
pager.Controls.Add(lb);


}
}
private void IB1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
this.DataGrid1.CurrentPageIndex = 0;
LoadData();
}
private void IB4_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
int PagaIndex = this.DataGrid1.PageCount - 1;
this.DataGrid1.CurrentPageIndex = PagaIndex;
LoadData();
}
private void IB2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
int PagaIndex = this.DataGrid1.CurrentPageIndex - 1;
this.DataGrid1.CurrentPageIndex = PagaIndex;
LoadData();
}

private void IB3_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
int PagaIndex = this.DataGrid1.CurrentPageIndex + 1;


this.DataGrid1.CurrentPageIndex = PagaIndex;
LoadData();
}
private void bt_Click(object sender, System.EventArgs e)
{
int PagaIndex = -1;
try
{
PagaIndex = int.Parse(((TextBox)this.DataGrid1.Controls[0].Controls[this.DataGrid1.Controls[0].Controls.Count - 1].Controls[0].FindControl( "tb ")).Text);
}
catch
{ }

if (PagaIndex > this.DataGrid1.PageCount)
{
PagaIndex = this.DataGrid1.PageCount;
}

PagaIndex = PagaIndex - 1;

if (PagaIndex < 0)
{
Page.RegisterClientScriptBlock( "errx ", " <script language=javascript> alert( '页码输入有误,请重输 ') </script> ");
}
else
{
this.DataGrid1.CurrentPageIndex = PagaIndex;
LoadData();
}
}

}
[解决办法]
帮顶
[解决办法]
up!
[解决办法]
提示: 在RowDataBound事件中判断e.Row的类型,如果是Footer,则对其进行赋值.

在这里面你必然知道当前是第几页,所以也就可以Render出你要的脚来,加图片加Flash上去都行......剩下的工作就是体力活了.
[解决办法]
收藏
[解决办法]
帮顶!
[解决办法]
(1) 当前页
<asp:Label ID= "LabelCurrentPage " runat= "server "
Text= " <%# ((GridView)Container.NamingContainer).PageIndex + 1 %> "> </asp:Label>


(2) 总页数
<asp:Label ID= "LabelPageCount " runat= "server "
Text= " <%# ((GridView)Container.NamingContainer).PageCount %> "> </asp:Label>

(3) 首页、上一页、下一页、尾页
<asp:LinkButton ID= "LinkButtonFirstPage " runat= "server " CommandArgument= "First " CommandName= "Page "
Visible= " <%# ((GridView)Container.NamingContainer).PageIndex != 0 %> "> 首页 </asp:LinkButton>

<asp:LinkButton ID= "LinkButtonPreviousPage " runat= "server " CommandArgument= "Prev " CommandName= "Page "
Visible= " <%# ((GridView)Container.NamingContainer).PageIndex != 0 %> "> 上一页 </asp:LinkButton>

<asp:LinkButton ID= "LinkButtonNextPage " runat= "server " CommandArgument= "Next " CommandName= "Page "
Visible= " <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %> "> 下一页 </asp:LinkButton>

<asp:LinkButton ID= "LinkButtonLastPage " runat= "server " CommandArgument= "Last " CommandName= "Page "
Visible= " <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %> "> 尾页 </asp:LinkButton>
<asp:textbox id= "txtNewPageIndex " runat= "server " width= "20px " text= ' <%# ((GridView)Container.Parent.Parent).PageIndex + 1 %> ' />
<asp:linkbutton id= "btnGo " runat= "server " causesvalidation= "False " commandargument= "-1 " commandname= "Page " text= "GO " />



注: 将上述代码放在GridView的 <PagerTemplate> </PagerTemplate>

PageIndexChanging事件中加入如下代码
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView theGrid = sender as GridView;
int newPageIndex = 0;
if (-2 == e.NewPageIndex) {
TextBox txtNewPageIndex = null;
GridViewRow pagerRow = theGrid.BottomPagerRow;
if (null != pagerRow) {
txtNewPageIndex = pagerRow.FindControl( "txtNewPageIndex ") as TextBox; }
if (null != txtNewPageIndex) {
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
}
}
else { newPageIndex = e.NewPageIndex;
}
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex > = theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
theGrid.PageIndex = newPageIndex;

}

[解决办法]
上面这个直接放在你的HTML页中就可以了注:放在GridView的 <PagerTemplate> </PagerTemplate>
中,在加事件就可以了,有一个问题就是在跳转页时输入字符出错,验证一下就行了
[解决办法]
这就是你要的效果啊,只需要写几行代码就行:http://www.webdiyer.com
[解决办法]
自己写一个分页控件就可以了。
[解决办法]
收藏
[解决办法]
关注中!~
[解决办法]
关注一下

读书人网 >asp.net

热点推荐