自定义控件开发之----自动套用格式

   在前两篇文章中,本人开发了一个基于WEB架构的分页控件,非常常见的控件。

 

 

   本人不才,当你用gridview的时候,把控件放进页面的时候,控件右上角都会出现一个小三角,点击就是自动套用格式,点击不同的格式在右边可以直观的看效果。

 

   以上的控件以前是默认只显示中文样式,后来从网上看到别人的文章用过类似的方法,所在就想给自己的控件也增加一个英文样式的。

 

   图例:

 

 

 

    怎样才能让控件具备这种功能呢?

 

   第一步:

 

      [Designer(typeof(AspNetPagerDesigner)), ToolboxData("<{0}:WebCustomControlPager runat=server></{0}:WebCustomControlPager>")]

   上面红色部分说明此控件支持设计模式编辑。

 

  第二步:

 

   AspNetPagerDesigner类 
   继承ControlDesigner
ControlDesigner作为web服务器控件的设计器的基类,通过继承此类,你可以实现需要的设计时效果。如果你要继承类似Label这样的控件.
  
Code
[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("中文样式"));
                }

                
return _dafc;
            }

        }

    }

    类:AspNetPagerAutoFormat : DesignerAutoFormat

继承DesignerAutoFormat类,重写Apply(Control control)方法,实现两种样式.

 

Code
public class AspNetPagerAutoFormat : DesignerAutoFormat
    
{
        
public AspNetPagerAutoFormat(string name) : base(name) { }
        
public override void Apply(Control control)
        
{
            
if (control is WebCustomControlPager)
            
{
                WebCustomControlPager aspNetPager 
= (WebCustomControlPager)control;
                
if (this.Name == "英文样式")
                
{
                    aspNetPager.goFirstText  
= "First";
                    aspNetPager.PrevPageText  
= "Prev";
                    aspNetPager.NextPageText  
= "Next";
                    aspNetPager.LastPageText 
= "Last";
                    aspNetPager.sGoText 
= "Go";
                    aspNetPager.sRecordCountText 
= "Total:";
                    aspNetPager.iRowsCount  
= 10;
                    aspNetPager.iRecordCount  
= 150;
                }

                
else if (this.Name == "中文样式")
                
{
                    aspNetPager.goFirstText 
= "首页";
                    aspNetPager.PrevPageText 
= "&lt;&lt;上一页";
                    aspNetPager.NextPageText 
= "下一页&gt;&gt;";
                    aspNetPager.LastPageText 
= "末页";
                    aspNetPager.sGoText 
= "跳转";
                    aspNetPager.iRowsCount 
= 10;
                    aspNetPager.iRecordCount 
= 150;
                    aspNetPager.currentPageText 
= "当前页:";
                    aspNetPager.sRecordCountText 
= "记录总数:";
                }

                
else
                
{
                    aspNetPager.goFirstText 
= "首页";
                    aspNetPager.PrevPageText 
= "&lt;&lt;上一页";
                    aspNetPager.NextPageText 
= "下一页&gt;&gt;";
                    aspNetPager.LastPageText 
= "末页";
                    aspNetPager.sGoText 
= "跳转";
                    aspNetPager.iRowsCount 
= 10;
                    aspNetPager.iRecordCount 
= 150;
                    aspNetPager.currentPageText 
= "当前页:";
                    aspNetPager.sRecordCountText 
= "记录总数:";
                
                }

            }

            
else
            
{
                
throw new Exception("The method or operation is not implemented.");
            }

        }

 

转载于:https://www.cnblogs.com/ASPNET2008/archive/2008/04/10/1147184.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值