一、解决ToolboxData元数据与Designer元数据冲突的问题
上一篇文章《千万级数据分页之三----实现设计时支持和数字分页 》 ,实现了设计时支持和数字分页,但还是有些不完善的地方,比如“自动套用格式”有“英文样式”和“字符样式”,但是如果我不想要这两种样式,即需要恢复初始状态时,需要再加入“默认样式”,在AspNetPagerAutoFormat类中加入默认样式。代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.Web.UI.WebControls;
using System.Drawing;
namespace CustomControls
...{
public class AspNetPagerAutoFormat : DesignerAutoFormat
...{
public AspNetPagerAutoFormat(string name) : base(name) ...{ }
public override void Apply(Control control)
...{
if (control is AspNetPager)
...{
AspNetPager aspNetPager = (AspNetPager)control;
if (this.Name == "英文样式")
...{
aspNetPager.ButtonText = "Go";
aspNetPager.FirstPageText = "First";
aspNetPager.PrePageText = "Prev";
aspNetPager.NextPageText = "Next";
aspNetPager.EndPageText = "End";
aspNetPager.LabelStyle.ForeColor = Color.Blue;
aspNetPager.LabelStyle.Font.Bold = true;
aspNetPager.TextBoxStyle.CssClass = "blue_rounded";
aspNetPager.TextBoxStyle.Width = Unit.Parse("50px");
aspNetPager.PageSize = 20;
aspNetPager.RecordCount = 0;
}
else if (this.Name == "符号样式")
...{
aspNetPager.ButtonText = "转到";
aspNetPager.FirstPageText = "<font face=webdings color="red">9</font>";
aspNetPager.PrePageText = "<font face=webdings color="red">7</font>";
aspNetPager.NextPageText = "<font face=webdings color="red">8</font>";
aspNetPager.EndPageText = "<font face=webdings color="red">:</font>";
aspNetPager.LabelStyle.ForeColor = Color.Red;
aspNetPager.LabelStyle.Font.Bold = true;
aspNetPager.TextBoxStyle.CssClass = "blue_rounded";
aspNetPager.TextBoxStyle.Width = Unit.Parse("40px");
aspNetPager.PageSize = 30;
}
else if (this.Name == "默认样式")
...{
aspNetPager.ButtonText = "Go";
aspNetPager.FirstPageText = "首页";
aspNetPager.PrePageText = "上一页";
aspNetPager.NextPageText = "下一页";
aspNetPager.EndPageText = "末页";
aspNetPager.LabelStyle.ForeColor = Color.Blue;
aspNetPager.LabelStyle.Font.Bold = true;
aspNetPager.TextBoxStyle.CssClass = "blue_rounded";
aspNetPager.TextBoxStyle.Width = Unit.Parse("40px");
aspNetPager.PageSize = 20;
aspNetPager.RecordCount = 0;
}
}
else
...{
throw new Exception("The method or operation is not implemented.");
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.Web.UI.WebControls;
using System.Drawing;
namespace CustomControls
...{
public class AspNetPagerAutoFormat : DesignerAutoFormat
...{
public AspNetPagerAutoFormat(string name) : base(name) ...{ }
public override void Apply(Control control)
...{
if (control is AspNetPager)
...{
AspNetPager aspNetPager = (AspNetPager)control;
if (this.Name == "英文样式")
...{
aspNetPager.ButtonText = "Go";
aspNetPager.FirstPageText = "First";
aspNetPager.PrePageText = "Prev";
aspNetPager.NextPageText = "Next";
aspNetPager.EndPageText = "End";
aspNetPager.LabelStyle.ForeColor = Color.Blue;
aspNetPager.LabelStyle.Font.Bold = true;
aspNetPager.TextBoxStyle.CssClass = "blue_rounded";
aspNetPager.TextBoxStyle.Width = Unit.Parse("50px");
aspNetPager.PageSize = 20;
aspNetPager.RecordCount = 0;
}
else if (this.Name == "符号样式")
...{
aspNetPager.ButtonText = "转到";
aspNetPager.FirstPageText = "<font face=webdings color="red">9</font>";
aspNetPager.PrePageText = "<font face=webdings color="red">7</font>";
aspNetPager.NextPageText = "<font face=webdings color="red">8</font>";
aspNetPager.EndPageText = "<font face=webdings color="red">:</font>";
aspNetPager.LabelStyle.ForeColor = Color.Red;
aspNetPager.LabelStyle.Font.Bold = true;
aspNetPager.TextBoxStyle.CssClass = "blue_rounded";
aspNetPager.TextBoxStyle.Width = Unit.Parse("40px");
aspNetPager.PageSize = 30;
}
else if (this.Name == "默认样式")
...{
aspNetPager.ButtonText = "Go";
aspNetPager.FirstPageText = "首页";
aspNetPager.PrePageText = "上一页";
aspNetPager.NextPageText = "下一页";
aspNetPager.EndPageText = "末页";
aspNetPager.LabelStyle.ForeColor = Color.Blue;
aspNetPager.LabelStyle.Font.Bold = true;
aspNetPager.TextBoxStyle.CssClass = "blue_rounded";
aspNetPager.TextBoxStyle.Width = Unit.Parse("40px");
aspNetPager.PageSize = 20;
aspNetPager.RecordCount = 0;
}
}
else
...{
throw new Exception("The method or operation is not implemented.");
}
}
}
}
然后在AspNetPagerDesigner类中加入默认样式,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.Design;
using System.ComponentModel.Design;
namespace CustomControls
...{
[SupportsPreviewControl(true)]
public class AspNetPagerDesigner : ControlDesigner
...{
private DesignerAutoFormatCollection _dafc;
public override DesignerAutoFormatCollection AutoFormats
...{
get
...{
if (_dafc == null)
...{
_dafc = new DesignerAutoFormatCollection();
_dafc.Add(new AspNetPagerAutoFormat("英文样式"));
_dafc.Add(new AspNetPagerAutoFormat("符号样式"));
_dafc.Add(new AspNetPagerAutoFormat("默认样式"));
}
return _dafc;
}
}
public override DesignerActionListCollection ActionLists
...{
get
...{
DesignerActionListCollection actionLists = new DesignerActionListCollection();
actionLists.Add(new AspNetPagerActionList(this));
return actionLists;
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.Design;
using System.ComponentModel.Design;
namespace CustomControls
...{
[SupportsPreviewControl(true)]
public class AspNetPagerDesigner : ControlDesigner
...{
private DesignerAutoFormatCollection _dafc;
public override DesignerAutoFormatCollection AutoFormats
...{
get
...{
if (_dafc == null)
...{
_dafc = new DesignerAutoFormatCollection();
_dafc.Add(new AspNetPagerAutoFormat("英文样式"));
_dafc.Add(new AspNetPagerAutoFormat("符号样式"));
_dafc.Add(new AspNetPagerAutoFormat("默认样式"));
}
return _dafc;
}
}
public override DesignerActionListCollection ActionLists
...{
get
...{
DesignerActionListCollection actionLists = new DesignerActionListCollection();
actionLists.Add(new AspNetPagerActionList(this));
return actionLists;
}
}
}
}
设计效果如下:
当点击默认样式的时候,发现分页控件里面的“首页”,“上一页”等分页文本不见了。这种情况很让人纳闷。
我想了很久也没找出是什么原因造成的。
而我去掉以下代码中的ToolboxData("<{0}:AspNetPager runat='server' PageSize='25' FirstPageText='首页' PrePageText='上一页' NextPageText='下一页' EndPageText='末页' ButtonText='GO'></{0}:AspNetPager>")出现了以下错误,如图所示:
[Designer(typeof(AspNetPagerDesigner)),ToolboxData("<{0}:AspNetPager runat='server' PageSize='25' FirstPageText='首页' PrePageText='上一页' NextPageText='下一页' EndPageText='末页' ButtonText='GO'></{0}:AspNetPager>")]
最后我通过在构造函数里对相关属性进行初始化才解决了这个问题。
public AspNetPager()
...{
PageIndex = 1;
PageSize = 20;
FirstPageText = "首页";
PrePageText = "上一页";
NextPageText = "下一页";
EndPageText = "末页";
ButtonText = "GO";
}
...{
PageIndex = 1;
PageSize = 20;
FirstPageText = "首页";
PrePageText = "上一页";
NextPageText = "下一页";
EndPageText = "末页";
ButtonText = "GO";
}
效果如下:
原因我一直没能搞明白,希望了解的兄弟告诉一下,谢谢!
二、增加分页模式
在分页控件中加入一个属性PageMode,类型为枚举:
[Category("Behavior"),Description("设置分页模式"),DefaultValue(AspNetPageMode.Comprehensive)]
public virtual AspNetPageMode PageMode
...{
get
...{
return ViewState["PageMode"] != null ? (AspNetPageMode)ViewState["PageMode"] : AspNetPageMode.Comprehensive;
}
set
...{
ViewState["PageMode"] = value;
}
}
public virtual AspNetPageMode PageMode
...{
get
...{
return ViewState["PageMode"] != null ? (AspNetPageMode)ViewState["PageMode"] : AspNetPageMode.Comprehensive;
}
set
...{
ViewState["PageMode"] = value;
}
}
其中AspNetPageMode定义如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace CustomControls
...{
/**//// <summary>
/// 分页模式
/// </summary>
public enum
using System.Collections.Generic;
using System.Text;
namespace CustomControls
...{
/**//// <summary>
/// 分页模式
/// </summary>
public enum