.net分頁控件
public class CPager2 : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public CPager2()
{
FirstImage = "../image/backward24.gif";
PreviousImage = "../image/back24.gif";
NextImage = "../image/next24.gif";
EndImage = "../image/forward24.gif";
FirstImage2 = "../image/backward24-gray.gif";
PreviousImage2 = "../image/back24-gray.gif";
NextImage2 = "../image/next24-gray.gif";
EndImage2 = "../image/forward24-gray.gif";
CreatePage();
}
#region 創始
private void CreatePage()
{
this.CssClass = "Page2";
//第一頁
ImageButton FirstBtn = new ImageButton();
FirstBtn.ID = "First";
FirstBtn.CommandName = "First";
FirstBtn.CssClass = "PageButton";
FirstBtn.ImageUrl = FirstImage;
this.Controls.Add(FirstBtn);
FirstBtn.Enabled = false;
FirstBtn.Command += new CommandEventHandler(Button_Command);
ImageButton preBtn = new ImageButton();
preBtn.ID = "Previous";
preBtn.CommandName = "Previous";
preBtn.CssClass = "PageButton";
preBtn.ImageUrl = PreviousImage;
this.Controls.Add(preBtn);
preBtn.Enabled = false;
preBtn.Command += new CommandEventHandler(Button_Command);
//----------------------------------------------------------------------------------------------------------------
//下一頁
ImageButton nextBtn = new ImageButton();
nextBtn.ID = "Next";
nextBtn.CommandName = "Next";
nextBtn.CssClass = "PageButton";
this.Controls.Add(nextBtn);
nextBtn.ImageUrl = NextImage;
nextBtn.Command += new CommandEventHandler(Button_Command);
ImageButton EndBtn = new ImageButton();
EndBtn.ID = "End";
EndBtn.CommandName = "End";
EndBtn.CssClass = "PageButton";
EndBtn.ImageUrl = EndImage;
this.Controls.Add(EndBtn);
EndBtn.Command += new CommandEventHandler(Button_Command);
DropDownList drp = new DropDownList();
drp.AutoPostBack = true;
drp.ID = "ddlGo";
drp.CssClass = "PageGo";
this.Controls.Add(drp);
drp.SelectedIndexChanged += new EventHandler(drp_SelectedIndexChanged);
}
#region Command
void Button_Command(object sender, CommandEventArgs e)
{
PageChangedEventArgs ex = new PageChangedEventArgs();
ex.OldPageIndex = PageIndex;
switch (e.CommandName)
{
case "First":
PageIndex = 0;
break;
case "Next":
PageIndex = PageIndex + 1;
break;
case "Previous":
PageIndex = PageIndex - 1;
break;
case "End":
PageIndex = PageTotal - 1;
break;
}
PageButtonStatus();
ex.NewPageIndex = PageIndex;
OnPageIndexChange(ex);
}
#endregion
#region 設置狀態
private void PageButtonStatus()
{
if (PageIndex <= 0)
{
((ImageButton)this.FindControl("First")).Enabled = false;
((ImageButton)this.FindControl("Previous")).Enabled = false;
((ImageButton)this.FindControl("First")).ImageUrl = FirstImage2;
((ImageButton)this.FindControl("Previous")).ImageUrl = PreviousImage2;
}
else
{
((ImageButton)this.FindControl("First")).Enabled = true;
((ImageButton)this.FindControl("Previous")).Enabled = true;
((ImageButton)this.FindControl("First")).ImageUrl = FirstImage;
((ImageButton)this.FindControl("Previous")).ImageUrl = PreviousImage;
}
if (PageIndex >= PageTotal - 1)
{
((ImageButton)this.FindControl("Next")).Enabled = false;
((ImageButton)this.FindControl("End")).Enabled = false;
((ImageButton)this.FindControl("Next")).ImageUrl = NextImage2;
((ImageButton)this.FindControl("End")).ImageUrl = EndImage2;
}
else
{
((ImageButton)this.FindControl("Next")).Enabled = true;
((ImageButton)this.FindControl("End")).Enabled = true;
((ImageButton)this.FindControl("End")).ImageUrl = EndImage;
((ImageButton)this.FindControl("Next")).ImageUrl = NextImage;
}
DropDownList ddlGo = (DropDownList)FindControl("ddlGo");
ddlGo.Items.Clear();
for (int i = 1; i <=PageTotal; i++)
{
ddlGo.Items.Add(i.ToString());
}
if(PageTotal>0)
ddlGo.SelectedIndex = PageIndex;
}
#endregion
void drp_SelectedIndexChanged(object sender, EventArgs e)
{
PageChangedEventArgs ex = new PageChangedEventArgs();
ex.OldPageIndex = PageIndex;
DropDownList ddlGo = (DropDownList)FindControl("ddlGo");
PageIndex = ddlGo.SelectedIndex;
PageButtonStatus();
ex.NewPageIndex = PageIndex;
OnPageIndexChange(ex);
}
#endregion
#region 屬性
[Description("當前頁")]
public int PageIndex
{
get
{
int temp = 0;
if (ViewState["PageIndex"] != null)
temp = Convert.ToInt32(ViewState["PageIndex"]);
return temp;
}
set
{
ViewState["PageIndex"] = value;
}
}
/// <summary>
/// 設置總頁數
/// </summary>
[Description("總頁數")]
public int PageTotal
{
get
{
if (ViewState["PageTotal"] != null)
return Convert.ToInt32((ViewState["PageTotal"]));
else
return 0;
}
set
{
ViewState["PageTotal"] = value;
this.PageButtonStatus();
}
}
[Category("設置圖片")]
[Description("第一頁圖片")]
public string FirstImage
{
get
{
string temp = "";
if (ViewState["FirstImage"] != null)
temp = Convert.ToString(ViewState["FirstImage"]);
return temp;
}
set
{
ViewState["FirstImage"] = value;
}
}
[Category("設置圖片")]
[Description("上一頁圖片")]
public string PreviousImage
{
get
{
string temp = "";
if (ViewState["PriviousImage"] != null)
temp = Convert.ToString(ViewState["PriviousImage"]);
return temp;
}
set
{
ViewState["PriviousImage"] = value;
}
}
[Category("設置圖片")]
[Description("下一頁圖片")]
public string NextImage
{
get
{
string temp = "";
if (ViewState["NextImage"] != null)
temp = Convert.ToString(ViewState["NextImage"]);
return temp;
}
set
{
ViewState["NextImage"] = value;
}
}
[Category("設置圖片")]
[Description("末頁圖片")]
public string EndImage
{
get
{
string temp = "";
if (ViewState["EndImage"] != null)
temp = Convert.ToString(ViewState["EndImage"]);
return temp;
}
set
{
ViewState["EndImage"] = value;
}
}
[Category("設置圖片")]
[Description("第一頁圖片")]
public string FirstImage2
{
get
{
string temp = "";
if (ViewState["FirstImage2"] != null)
temp = Convert.ToString(ViewState["FirstImage2"]);
return temp;
}
set
{
ViewState["FirstImage2"] = value;
}
}
[Category("設置圖片")]
[Description("上一頁圖片")]
public string PreviousImage2
{
get
{
string temp = "";
if (ViewState["PriviousImage2"] != null)
temp = Convert.ToString(ViewState["PriviousImage2"]);
return temp;
}
set
{
ViewState["PriviousImage2"] = value;
}
}
[Category("設置圖片")]
[Description("下一頁圖片")]
public string NextImage2
{
get
{
string temp = "";
if (ViewState["NextImage2"] != null)
temp = Convert.ToString(ViewState["NextImage2"]);
return temp;
}
set
{
ViewState["NextImage2"] = value;
}
}
[Category("設置圖片")]
[Description("末頁圖片")]
public string EndImage2
{
get
{
string temp = "";
if (ViewState["EndImage2"] != null)
temp = Convert.ToString(ViewState["EndImage2"]);
return temp;
}
set
{
ViewState["EndImage2"] = value;
}
}
#endregion
#region 事件
public delegate void PageChangedEventHandler(object sender, PageChangedEventArgs e);
public event PageChangedEventHandler PageIndexChanged;
protected virtual void OnPageIndexChange(PageChangedEventArgs e)
{
if (PageIndexChanged != null)
PageIndexChanged(this, e);
}
#endregion
}
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public CPager2()
{
FirstImage = "../image/backward24.gif";
PreviousImage = "../image/back24.gif";
NextImage = "../image/next24.gif";
EndImage = "../image/forward24.gif";
FirstImage2 = "../image/backward24-gray.gif";
PreviousImage2 = "../image/back24-gray.gif";
NextImage2 = "../image/next24-gray.gif";
EndImage2 = "../image/forward24-gray.gif";
CreatePage();
}
#region 創始
private void CreatePage()
{
this.CssClass = "Page2";
//第一頁
ImageButton FirstBtn = new ImageButton();
FirstBtn.ID = "First";
FirstBtn.CommandName = "First";
FirstBtn.CssClass = "PageButton";
FirstBtn.ImageUrl = FirstImage;
this.Controls.Add(FirstBtn);
FirstBtn.Enabled = false;
FirstBtn.Command += new CommandEventHandler(Button_Command);
ImageButton preBtn = new ImageButton();
preBtn.ID = "Previous";
preBtn.CommandName = "Previous";
preBtn.CssClass = "PageButton";
preBtn.ImageUrl = PreviousImage;
this.Controls.Add(preBtn);
preBtn.Enabled = false;
preBtn.Command += new CommandEventHandler(Button_Command);
//----------------------------------------------------------------------------------------------------------------
//下一頁
ImageButton nextBtn = new ImageButton();
nextBtn.ID = "Next";
nextBtn.CommandName = "Next";
nextBtn.CssClass = "PageButton";
this.Controls.Add(nextBtn);
nextBtn.ImageUrl = NextImage;
nextBtn.Command += new CommandEventHandler(Button_Command);
ImageButton EndBtn = new ImageButton();
EndBtn.ID = "End";
EndBtn.CommandName = "End";
EndBtn.CssClass = "PageButton";
EndBtn.ImageUrl = EndImage;
this.Controls.Add(EndBtn);
EndBtn.Command += new CommandEventHandler(Button_Command);
DropDownList drp = new DropDownList();
drp.AutoPostBack = true;
drp.ID = "ddlGo";
drp.CssClass = "PageGo";
this.Controls.Add(drp);
drp.SelectedIndexChanged += new EventHandler(drp_SelectedIndexChanged);
}
#region Command
void Button_Command(object sender, CommandEventArgs e)
{
PageChangedEventArgs ex = new PageChangedEventArgs();
ex.OldPageIndex = PageIndex;
switch (e.CommandName)
{
case "First":
PageIndex = 0;
break;
case "Next":
PageIndex = PageIndex + 1;
break;
case "Previous":
PageIndex = PageIndex - 1;
break;
case "End":
PageIndex = PageTotal - 1;
break;
}
PageButtonStatus();
ex.NewPageIndex = PageIndex;
OnPageIndexChange(ex);
}
#endregion
#region 設置狀態
private void PageButtonStatus()
{
if (PageIndex <= 0)
{
((ImageButton)this.FindControl("First")).Enabled = false;
((ImageButton)this.FindControl("Previous")).Enabled = false;
((ImageButton)this.FindControl("First")).ImageUrl = FirstImage2;
((ImageButton)this.FindControl("Previous")).ImageUrl = PreviousImage2;
}
else
{
((ImageButton)this.FindControl("First")).Enabled = true;
((ImageButton)this.FindControl("Previous")).Enabled = true;
((ImageButton)this.FindControl("First")).ImageUrl = FirstImage;
((ImageButton)this.FindControl("Previous")).ImageUrl = PreviousImage;
}
if (PageIndex >= PageTotal - 1)
{
((ImageButton)this.FindControl("Next")).Enabled = false;
((ImageButton)this.FindControl("End")).Enabled = false;
((ImageButton)this.FindControl("Next")).ImageUrl = NextImage2;
((ImageButton)this.FindControl("End")).ImageUrl = EndImage2;
}
else
{
((ImageButton)this.FindControl("Next")).Enabled = true;
((ImageButton)this.FindControl("End")).Enabled = true;
((ImageButton)this.FindControl("End")).ImageUrl = EndImage;
((ImageButton)this.FindControl("Next")).ImageUrl = NextImage;
}
DropDownList ddlGo = (DropDownList)FindControl("ddlGo");
ddlGo.Items.Clear();
for (int i = 1; i <=PageTotal; i++)
{
ddlGo.Items.Add(i.ToString());
}
if(PageTotal>0)
ddlGo.SelectedIndex = PageIndex;
}
#endregion
void drp_SelectedIndexChanged(object sender, EventArgs e)
{
PageChangedEventArgs ex = new PageChangedEventArgs();
ex.OldPageIndex = PageIndex;
DropDownList ddlGo = (DropDownList)FindControl("ddlGo");
PageIndex = ddlGo.SelectedIndex;
PageButtonStatus();
ex.NewPageIndex = PageIndex;
OnPageIndexChange(ex);
}
#endregion
#region 屬性
[Description("當前頁")]
public int PageIndex
{
get
{
int temp = 0;
if (ViewState["PageIndex"] != null)
temp = Convert.ToInt32(ViewState["PageIndex"]);
return temp;
}
set
{
ViewState["PageIndex"] = value;
}
}
/// <summary>
/// 設置總頁數
/// </summary>
[Description("總頁數")]
public int PageTotal
{
get
{
if (ViewState["PageTotal"] != null)
return Convert.ToInt32((ViewState["PageTotal"]));
else
return 0;
}
set
{
ViewState["PageTotal"] = value;
this.PageButtonStatus();
}
}
[Category("設置圖片")]
[Description("第一頁圖片")]
public string FirstImage
{
get
{
string temp = "";
if (ViewState["FirstImage"] != null)
temp = Convert.ToString(ViewState["FirstImage"]);
return temp;
}
set
{
ViewState["FirstImage"] = value;
}
}
[Category("設置圖片")]
[Description("上一頁圖片")]
public string PreviousImage
{
get
{
string temp = "";
if (ViewState["PriviousImage"] != null)
temp = Convert.ToString(ViewState["PriviousImage"]);
return temp;
}
set
{
ViewState["PriviousImage"] = value;
}
}
[Category("設置圖片")]
[Description("下一頁圖片")]
public string NextImage
{
get
{
string temp = "";
if (ViewState["NextImage"] != null)
temp = Convert.ToString(ViewState["NextImage"]);
return temp;
}
set
{
ViewState["NextImage"] = value;
}
}
[Category("設置圖片")]
[Description("末頁圖片")]
public string EndImage
{
get
{
string temp = "";
if (ViewState["EndImage"] != null)
temp = Convert.ToString(ViewState["EndImage"]);
return temp;
}
set
{
ViewState["EndImage"] = value;
}
}
[Category("設置圖片")]
[Description("第一頁圖片")]
public string FirstImage2
{
get
{
string temp = "";
if (ViewState["FirstImage2"] != null)
temp = Convert.ToString(ViewState["FirstImage2"]);
return temp;
}
set
{
ViewState["FirstImage2"] = value;
}
}
[Category("設置圖片")]
[Description("上一頁圖片")]
public string PreviousImage2
{
get
{
string temp = "";
if (ViewState["PriviousImage2"] != null)
temp = Convert.ToString(ViewState["PriviousImage2"]);
return temp;
}
set
{
ViewState["PriviousImage2"] = value;
}
}
[Category("設置圖片")]
[Description("下一頁圖片")]
public string NextImage2
{
get
{
string temp = "";
if (ViewState["NextImage2"] != null)
temp = Convert.ToString(ViewState["NextImage2"]);
return temp;
}
set
{
ViewState["NextImage2"] = value;
}
}
[Category("設置圖片")]
[Description("末頁圖片")]
public string EndImage2
{
get
{
string temp = "";
if (ViewState["EndImage2"] != null)
temp = Convert.ToString(ViewState["EndImage2"]);
return temp;
}
set
{
ViewState["EndImage2"] = value;
}
}
#endregion
#region 事件
public delegate void PageChangedEventHandler(object sender, PageChangedEventArgs e);
public event PageChangedEventHandler PageIndexChanged;
protected virtual void OnPageIndexChange(PageChangedEventArgs e)
{
if (PageIndexChanged != null)
PageIndexChanged(this, e);
}
#endregion
}