📄 mydatagrid.cs
字号:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace MyControls
{
/// <summary>
/// MyDataGrid 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:MyDataGrid runat=server></{0}:MyDataGrid>")]
public class MyDataGrid : DataGrid
{
public const string MyDataGridPageEventCommandName = "MyPage";
public const string FisrtPageText = "首页";
public const string FisrtPageCommandArgument = "Fisrt";
public const string EndPageText = "尾页";
public const string EndPageCommandArgument = "End";
protected override void OnItemCreated(DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Pager)
{
if (this.AllowPaging && this.PagerStyle.Mode == PagerMode.NextPrev)
{
if (this.CurrentPageIndex > 0)
{
LinkButton lb;
lb = new LinkButton();
lb.Text = FisrtPageText;
lb.CommandName = MyDataGridPageEventCommandName;
lb.CommandArgument = FisrtPageCommandArgument;
e.Item.Cells[0].Controls.AddAt(0, new LiteralControl(" "));
e.Item.Cells[0].Controls.AddAt(0, lb);
}
else
{
e.Item.Cells[0].Controls.AddAt(0, new LiteralControl(FisrtPageText + " "));
}
if (this.CurrentPageIndex < this.PageCount - 1)
{
LinkButton lb;
lb = new LinkButton();
lb.Text = EndPageText;
lb.CommandName = MyDataGridPageEventCommandName;
lb.CommandArgument = EndPageCommandArgument;
e.Item.Cells[0].Controls.Add(new LiteralControl(" "));
e.Item.Cells[0].Controls.Add(lb);
}
else
{
e.Item.Cells[0].Controls.Add(new LiteralControl(" " + EndPageText));
}
}
Label lab = new Label();
lab.Text = " 当前第" +
(CurrentPageIndex + 1)
+ "页,共" +
PageCount
+ "页 ";
e.Item.Cells[0].Controls.Add(lab);
DropDownList dl = new DropDownList();
dl.Items.Clear();
for (int i = 1; i <= PageCount; i++)
dl.Items.Add(i.ToString());
dl.SelectedValue = Convert.ToString(CurrentPageIndex + 1);
dl.AutoPostBack = true;
dl.SelectedIndexChanged += new EventHandler(dl_SelectedIndexChanged);
e.Item.Cells[0].Controls.Add(dl);
}
base.OnItemCreated(e);
}
protected override void OnItemCommand(DataGridCommandEventArgs e)
{
if (this.AllowPaging && this.PagerStyle.Mode == PagerMode.NextPrev)
{
if (e.CommandName == MyDataGridPageEventCommandName)
{
if (e.CommandArgument.Equals(FisrtPageCommandArgument))
{
OnPageIndexChanged(new DataGridPageChangedEventArgs(this, 0));
}
if (e.CommandArgument.Equals(EndPageCommandArgument))
{
OnPageIndexChanged(new DataGridPageChangedEventArgs(this, this.PageCount - 1));
}
}
}
base.OnItemCommand(e);
}
private void dl_SelectedIndexChanged(object sender, EventArgs e)
{
OnPageIndexChanged(new DataGridPageChangedEventArgs(this,
Convert.ToInt32(((DropDownList)sender).SelectedValue) - 1
));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -