想重写,gridview的分页事件, public class GridView : System.Web.UI.WebControls.GridView 定义了如上服务器控件类,重写了
protected override void OnDataBinding(EventArgs e)事件,定义了 public class GVPager : WebControl, ITemplate 类型,并如下实现,
public class GVPager : WebControl, ITemplate
{
private LinkButton lkbtnPre = new LinkButton();
public LinkButton LkbtnPre
{
get { return lkbtnPre; }
set { lkbtnPre = value; }
}
private LinkButton lkbtnFri = new LinkButton();
public LinkButton LkbtnFri
{
get { return lkbtnFri; }
set { lkbtnFri = value; }
}
private LinkButton lkbtnNex = new LinkButton();
public LinkButton LkbtnNex
{
get { return lkbtnNex; }
set { lkbtnNex = value; }
}
private LinkButton lkbtnLas = new LinkButton();
public LinkButton LkbtnLas
{
get { return lkbtnLas; }
set { lkbtnLas = value; }
}
#region ITemplate 成员
public void InstantiateIn(Control container)
{
lkbtnFri.Text = "首页";
lkbtnPre.Text = "上一页";
lkbtnNex.Text = "下一页";
lkbtnLas.Text = "尾页";
lkbtnFri.CommandArgument = "Fri";
lkbtnPre.CommandArgument = "Pre";
lkbtnNex.CommandArgument = "Nex";
lkbtnNex.CommandArgument = "Las";
lkbtnFri.Style.Add("text-decoration", "none");
lkbtnPre.Style.Add("text-decoration", "none");
lkbtnNex.Style.Add("text-decoration", "none");
lkbtnLas.Style.Add("text-decoration", "none");
container.Controls.Add(lkbtnFri);
container.Controls.Add(lkbtnPre);
container.Controls.Add(lkbtnNex);
container.Controls.Add(lkbtnLas);
}
#endregion
}
}并预定了分页事件:
protected void PagerClick(object sender, EventArgs e)
{
string commandName = ((LinkButton)sender).CommandArgument;
switch (commandName)
{
case "Fri": this.Page.Response.Write("1"); break;
case "Pre": this.Page.Response.Write("2"); break;
case "Nex": this.Page.Response.Write("3"); break;
case "Las": this.Page.Response.Write("4"); break;
}
this.DataBind();
}
在Bunding 中 :如下做
GVPager gvPager = new GVPager();
this.PagerTemplate = gvPager;
gvPager.LkbtnFri.Click += PagerClick;
gvPager.LkbtnPre.Click += PagerClick;
gvPager.LkbtnNex.Click += PagerClick;
gvPager.LkbtnLas.Click += PagerClick;
预期希望,给gvPager的四个按钮,添加预定的事件,PagerClick,但是,实际效果是,事件没添加上去 不知道这样做有什么不对,大家 还有好的办法没?
public class CustomGridView : GridView { private const string _virtualItemCount = "virtualItemCount"; private const string _currentPageIndex = "currentPageIndex"; [Browsable(true), Category("Custom")] [Description("Set the virtual item count for this grid")] public int VirtualItemCount { get { if (ViewState[_virtualItemCount] == null) ViewState[_virtualItemCount] = -1; return Convert.ToInt32(ViewState[_virtualItemCount]); } set { ViewState[_virtualItemCount] = value; } } private int CurrentPageIndex { get { if (ViewState[_currentPageIndex] == null) ViewState[_currentPageIndex] = 0; return Convert.ToInt32(ViewState[_currentPageIndex]); } set { ViewState[_currentPageIndex] = value; } } public override object DataSource { get { return base.DataSource; } set { base.DataSource = value; this.CurrentPageIndex = this.PageIndex; } } protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource) { if (CustomPaging) { pagedDataSource.VirtualCount = this.VirtualItemCount; pagedDataSource.CurrentPageIndex = this.CurrentPageIndex; } base.InitializePager(row, columnSpan, pagedDataSource); } public bool CustomPaging { get { return (this.VirtualItemCount != -1); } } }
//-----------------------------------------------------
protected void gvAccounts_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvAccounts.PageIndex = e.NewPageIndex; BindGrid(e.NewPageIndex, gvAccounts.PageSize); } protected void BindGrid(int pageIndex, int pageSize) { var accounts = AccountManager.GetAllAccounts<int>(pageIndex, pageSize, x=> x.AccountId); gvAccounts.VirtualItemCount = accounts.TotalCount; gvAccounts.DataSource = accounts; gvAccounts.DataBind(); }
请问,这个我没有看懂啊,你的PageIndex与PageSize 是在外部传进来的吧,不是用的GridView自身的PagerTemplate吧?求详细讲解下
@二进制小男人: 内部传递的:
protected void gvAccounts_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvAccounts.PageIndex = e.NewPageIndex; BindGrid(e.NewPageIndex, gvAccounts.PageSize); }
@荒野的呼唤: 分页按钮,例如 四个 首页,下一页,上一页,尾页 这些的单击事件,触发分页,怎么做的呢??例如我自定义了 GVPager 分页模板,请问您是怎么做的呢?
个人觉得GridView自带的分页控件除了样式难看外,也不好修改,而且还是个假分页,推荐用aspnetpager分页控件,已经是一个比较成熟的控件了
呵呵,我重写他,就是重写了他的样式,并实现真分页,这是我问什么修改它的原因。
在成熟的分页控件也不要用,当你面临大数据量访问的时候,如果你还是想用分页控件来实现的话,那你就想的太简单了,结果就是你会很惨,页面经常会卡死,分页是要用SQL语句去分页的。
-_-!我用的就是SQL分页,只是用控件显示。自动生成sql然后去分页,现在自动生成sql没问题,只是事件触发上的问题,你说的我明白。只是,公司叫这么用,我说了没人听。我怎么办?希望能回答我的问题。