我在页面中拖入了一个sqldatasource和一个gridView,为gridView设置了datasourceID,然后我后台写代码在grieView的导航去添加一个分页的页码显示,如(目前所在页码(1/3))和一个导航的linkButton按钮。代码如下。。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.Caption = "员工基本信息";
this.GridView1.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;
this.GridView1.PagerSettings.FirstPageImageUrl = "~/First.gif";
this.GridView1.PagerSettings.LastPageImageUrl = "~/Last.gif";
this.GridView1.PagerSettings.NextPageImageUrl = "~/Next.gif";
this.GridView1.PagerSettings.PreviousPageImageUrl = "~/Previous.gif";
this.GridView1.PageSize = 3;
}
this.GridView1.DataBind();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRow botomRow = this.GridView1.BottomPagerRow;
Label bottomPagerNo = new Label();
bottomPagerNo.Text = "目前所在的分页码(" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + ")";
botomRow.Cells[0].Controls.Add(bottomPagerNo); Literal myLiteral = new Literal();
myLiteral.Text = "<br/>";
botomRow.Cells[0].Controls.Add(myLiteral); for (int i = 0; i < this.GridView1.PageCount; i++)
{
LinkButton PageNo = new LinkButton();
PageNo.Text = Convert.ToString(i + 1);
PageNo.ID = PageNo + i.ToString();
PageNo.CommandArgument = i.ToString();
PageNo.Click += new EventHandler(PageNo_Click);
botomRow.Cells[0].Controls.Add(PageNo);
Literal bland = new Literal();
bland.Text = " ";
botomRow.Cells[0].Controls.Add(bland);
}
} void PageNo_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = Convert.ToInt16(((LinkButton)sender).CommandArgument);
}显示的效果为这种情况下。。当我单击下面代码添加的linkButton导航按钮时,可以正确导航分页,这说明每次单击了linkButton后,都执行了GridView1_DataBound事件,但是当我去掉Page_Load事件中的 this.GridView1.DataBind();这条语句后,再运行单击linkButton导航按钮,导航区中的页码显示和linkBUTTON按钮都会消失。。这说明了没有执行 this.GridView1.DataBind();事件。。但是我去单击导航区中的next,previous内置的导航按钮时。。下面的页码显示和linkBUTTON却不会消失我不明白的是它内置的导航区按钮会自动执行GridView1_DataBound事件,而代码添加进去的导航按钮必须每次页面加载都执行 this.GridView1.DataBind()才会执行GridView1_DataBound事件吗?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.Caption = "员工基本信息";
this.GridView1.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;
this.GridView1.PagerSettings.FirstPageImageUrl = "~/First.gif";
this.GridView1.PagerSettings.LastPageImageUrl = "~/Last.gif";
this.GridView1.PagerSettings.NextPageImageUrl = "~/Next.gif";
this.GridView1.PagerSettings.PreviousPageImageUrl = "~/Previous.gif";
this.GridView1.PageSize = 3;
}
this.GridView1.DataBind();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRow botomRow = this.GridView1.BottomPagerRow;
Label bottomPagerNo = new Label();
bottomPagerNo.Text = "目前所在的分页码(" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + ")";
botomRow.Cells[0].Controls.Add(bottomPagerNo); Literal myLiteral = new Literal();
myLiteral.Text = "<br/>";
botomRow.Cells[0].Controls.Add(myLiteral); for (int i = 0; i < this.GridView1.PageCount; i++)
{
LinkButton PageNo = new LinkButton();
PageNo.Text = Convert.ToString(i + 1);
PageNo.ID = PageNo + i.ToString();
PageNo.CommandArgument = i.ToString();
PageNo.Click += new EventHandler(PageNo_Click);
botomRow.Cells[0].Controls.Add(PageNo);
Literal bland = new Literal();
bland.Text = " ";
botomRow.Cells[0].Controls.Add(bland);
}
} void PageNo_Click(object sender, EventArgs e)
{
this.GridView1.PageIndex = Convert.ToInt16(((LinkButton)sender).CommandArgument);
}显示的效果为这种情况下。。当我单击下面代码添加的linkButton导航按钮时,可以正确导航分页,这说明每次单击了linkButton后,都执行了GridView1_DataBound事件,但是当我去掉Page_Load事件中的 this.GridView1.DataBind();这条语句后,再运行单击linkButton导航按钮,导航区中的页码显示和linkBUTTON按钮都会消失。。这说明了没有执行 this.GridView1.DataBind();事件。。但是我去单击导航区中的next,previous内置的导航按钮时。。下面的页码显示和linkBUTTON却不会消失我不明白的是它内置的导航区按钮会自动执行GridView1_DataBound事件,而代码添加进去的导航按钮必须每次页面加载都执行 this.GridView1.DataBind()才会执行GridView1_DataBound事件吗?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货