增加Pager控件没有的跳转页功能
作者: venjiang 2004年6月14日
Pager类,用于分页, Controls\Utility\Pager.cs
首先增加成员变量:
/// venjiang:增加跳转页输入文本框
/// </summary>
TextBox gotoPage;
/// <summary>
/// venjiang:增加跳转按钮
/// </summary>
LinkButton gotoButton;
其次增加控件:
/// <summary>
/// 跳转按钮
/// </summary>
void AddGotoButton()
{
this.gotoButton=new LinkButton();
this.gotoButton.ID="GoTo";
this.gotoButton.Text="Go";
this.gotoButton.Click += new System.EventHandler(PageIndex_Click);
Controls.Add(gotoButton);
}
/// <summary>
/// 增加跳转页输入框
/// </summary>
void AddGotoPage()
{
this.gotoPage=new TextBox();
this.gotoPage.Width=30;
this.gotoPage.ID="GoToPage";
//this.gotoPage.Text="3";
this.gotoPage.TextChanged += new System.EventHandler(PageText_Change);
//this.gotoPage.EnableViewState=true;
Controls.Add(gotoPage);
}
增加跳转页文本框文本改变事件:
void PageText_Change(Object sender,EventArgs e)
{
int i = 1;
try
{
i = Convert.ToInt32(this.gotoPage.Text.Trim().ToString());
if(i < 1)
{
i = 1;
this.gotoPage.Text="1";
}
if(i > CalculateTotalPages())
{
i = CalculateTotalPages();
this.gotoPage.Text=i.ToString();
}
}
catch
{
this.gotoPage.Text="";
return;
}
this.gotoButton.CommandArgument = (i-1).ToString();
}
增加呈现方法:
void RenderGotoPage(HtmlTextWriter writer)
{
Literal l,m;
l = new Literal();
l.Text = " ";
l.RenderControl(writer);
this.gotoPage.RenderControl(writer);
this.gotoButton.RenderControl(writer);
m = new Literal();
m.Text = " ";
m.RenderControl(writer);
}
最后将所创建的方法分别添加到相应用方法中:
在CreateChildControls()方法尾部,增加this.AddGotoPage();this.AddGotoButton();
Render(…)方法尾部,增加this.RenderGotoPage(writer);
重新运行程序,分页图如下: