知识大全 Asp.net 2.0 自定义控件开发

Posted

篇首语:不积跬步,无以至千里;不积小流,无以成江海。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Asp.net 2.0 自定义控件开发相关的知识,希望对你有一定的参考价值。

Asp.net 2.0 自定义控件开发  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  (一) 概述

  控件名称 工具栏控件 分为主菜单和子菜单部分 涉及到公司代码版权 目前版本不是最终版本 且去除了一些代码 后面可下载的仅为纯控件开发技术相关代码   通过扩展可以任意定制需要的功能 里面一些设计思想也具有参考价值

  (二) 控件运行效果截图

     默认样式和自定义样式

  

  

       项集合编辑器

  

  

  

  

    (三) 代码部分

   主控件类ToolButton代码:

      /// <summary>      /// Author: 【金鹰】【专注于DotNet技术】【ChengKing(ZhengJian)】      /// Blog:   Http:///ChengKing      /// </summary>            using System;      using System Collections Generic;      using System ComponentModel;      using System Text;     using System Web;     using System Web UI;     using System Web UI WebControls;     using System Web UI HtmlControls;     using System IO;     using System Drawing;          namespace ToolButton               [DefaultProperty( Text )]          [ToolboxData( < :ToolButton runat=server></ :ToolButton> )]          public class ToolButton : CompositeControl INamingContainer                        #region 内部变量                   //含下拉箭头单元格的ID              private string tcDownID = ;                   //本控件的ID              private string tbID = ;                   #endregion                   #region 属性                   /// <summary>              /// 设置或获取显示的文本              /// </summary>              [Bindable(true)]              [Category( Appearance )]              [DefaultValue( [按钮文本] )]              [Localizable(true)]              [Description( 显示的文本 )]              public string Text                                get                                        string s = (string)ViewState[ Text ];                      return ((s == null) ? String Empty : s);                                    set                                        ViewState[ Text ] = value;                                                   /// <summary>              /// 设置或获取图片路径              /// </summary>              [Bindable(true)]              [Category( Appearance )]              [DefaultValue( NoPic gif )]              [Localizable(true)]              [Description( 显示图像的文件名 )]              public string ImageName                                get                                        String s = (String)ViewState[ ImageName ];                      return ((s == null) ? String Empty : s);                                         set                                        ViewState[ ImageName ] = value;                                                   /// <summary>              /// 设置或获取图片文件夹目录              /// </summary>              [Bindable(true)]              [Category( 客户端路径 )]              [DefaultValue( )]              [Localizable(true)]              [Description( 资源(image/css/js)的客户端根目录 )]              public string ClientPath                                get                                        String s = (String)ViewState[ ClientPath ];                      return ((s == null) ? String Empty : s);                                         set                                        ViewState[ ClientPath ] = value;                                                  /// <summary>             ///  设置或获取点击按钮执行的命令(点击大按钮执行的命令)             /// </summary>             [Bindable(true)]             [Category( 行为 )]             [DefaultValue( )]             [Localizable(true)]             [Description( 点击控件执行的命令字串 )]             public string Command                              get                                      string s = (string)ViewState[ Command ];                     return ((s == null) ? String Empty : s);                                  set                                      ViewState[ Command ] = value;                                               private PopupMenu _Items = new PopupMenu();                 /// <summary>             /// 获取子菜单集合             /// </summary>             [PersistenceMode(PersistenceMode InnerDefaultProperty)]             [DesignerSerializationVisibility(DesignerSerializationVisibility Content)]             [Description( 获取或设置下拉子菜单项集合 )]             [Category( 杂项 )]             [NotifyParentProperty(true)]             [TypeConverter(typeof(CollectionConverter))]             [DesignOnly(false)]             public PopupMenu Items                              get                                      if (_Items == null)                                              _Items = new PopupMenu();                                          return _Items;                                               /// <summary>             /// 设置或获取子菜单集合句柄             /// </summary>             [Browsable(false)]             [Description( 获取或设置菜单 )]             public PopupMenu Menu                              get                                      return _Items;                                  set                                      this _Items Clear();                     foreach (PopupMenuItem item in value)                                              this _Items Add(item);                                                                    [Browsable(true)]             [Category( CSS样式 )]             [Description( 获取或设置主控件的OnMouseOver样式 )]             public string CSS_MAIN_ONMOUSEOVER_CLASSNAME                              get                                      string s = (string)ViewState[ CSS_MAIN_ONMOUSEOVER ];                     return ((s == null) ? String Empty : s);                                  set                                      ViewState[ CSS_MAIN_ONMOUSEOVER ] = value;                                           [Browsable(true)]             [Category( CSS样式 )]             [Description( 获取或设置主控件的OnMouseOut样式 )]             public string CSS_MAIN_ONMOUSEOUT_CLASSNAME                              get                                      string s = (string)ViewState[ CSS_MAIN_ONMOUSEOUT ];                     return ((s == null) ? String Empty : s);                                  set                                      ViewState[ CSS_MAIN_ONMOUSEOUT ] = value;                                           [Browsable(true)]             [Category( CSS样式 )]             [Description( 获取或设置下拉款项的OnMouseOver样式 )]             public string CSS_SUBITEM_ONMOUSEOVER_CLASSNAME                              get                                      string s = (string)ViewState[ CSS_SUBITEM_ONMOUSEOVER ];                     return ((s == null) ? String Empty : s);                                  set                                      ViewState[ CSS_SUBITEM_ONMOUSEOVER ] = value;                                           [Browsable(true)]             [Category( CSS样式 )]             [Description( 获取或设置下拉款项的OnMouseOut样式 )]             public string CSS_SUBITEM_ONMOUSEOUT_CLASSNAME                              get                                      string s = (string)ViewState[ CSS_SUBITEM_ONMOUSEOUT ];                     return ((s == null) ? String Empty : s);                                  set                                      ViewState[ CSS_SUBITEM_ONMOUSEOUT ] = value;                                                   #endregion                 #region 构造函数                 /// <summary>             /// 构造函数             /// </summary>             public ToolButton()                              //设置一套默认风格                 this BackColor = Color FromName( MenuBar );//ViewState[ BackColor ] = Color FromName( MenuBar );                 ViewState[ Text ] = [按钮文本] ;                 //ViewState[ ClientPath ] = ToolButtonImages ;                 ViewState[ ImageName ] = NoPic gif ;                 this BorderStyle = BorderStyle NotSet;//ViewState[ BorderStyle ] = BorderStyle NotSet;                 this BorderWidth = Unit Pixel( );//ViewState[ BorderWidth ] = Unit Pixel( );                 this BorderColor = Color FromName( ControlDark );//ViewState[ BorderColor ] = Color FromName( ControlDark );                 this Height = Unit Pixel( );//ViewState[ Height ] = Unit Pixel( );                 //this Width = Unit Pixel( );]                              #endregion                 #region 方法                 protected override void Render(HtmlTextWriter writer)                              //base Render(writer);                 PrepareControlForRendering();                 base RenderContents(writer);                              /// <summary>             /// 设置控件树的样式和客户端事件             /// </summary>             private void PrepareControlForRendering()                                  if (this Controls Count < )                                      return;                                      bool IsCustomStyle = CSS_MAIN_ONMOUSEOVER_CLASSNAME != String Empty && CSS_MAIN_ONMOUSEOUT_CLASSNAME != String Empty;                     Table t = (Table)this Controls[ ];                 t CellPadding = ;                 t CellSpacing = ;                 t Style Add( Cursor default );                 t CopyBaseAttributes(this);                 t Height = this Height;                 t Width = this Width;                 t BorderStyle = this BorderStyle;                 t BorderWidth = Unit Pixel( );                 t Attributes Add( onselectstart return false; );                     //t Style Add( aligh left );                 //t Style Add( table layout fixed );                 //t Style Add( word wrap break word );                 //***display:inline block );                     //if (CSS_MAIN_ONMOUSEOVER_CLASSNAME != String Empty && CSS_MAIN_ONMOUSEOUT_CLASSNAME != String Empty)                 //                 //    t CssClass = this CSS_MAIN_ONMOUSEOUT_CLASSNAME;                 //    t Attributes Add( onmouseenter ChangeClassName(this + CSS_MAIN_ONMOUSEOVER_CLASSNAME + ) );                 //    t Attributes Add( onmouseleave ChangeClassName(this + CSS_MAIN_ONMOUSEOUT_CLASSNAME + ) );                 //                         if (t Rows Count > )                                      TableRow tr = t Rows[ ];                     //if (IsCustomStyle == false)                     //                         tr BorderWidth = Unit Pixel( );                    //                              ////应用用户定义样式                     //if (IsCustomStyle)                     //                     //    tr CssClass = this CSS_MAIN_ONMOUSEOUT_CLASSNAME;                     //    tr Attributes Add( onmouseenter ChangeClassName(this + CSS_MAIN_ONMOUSEOVER_CLASSNAME + ) );                     //    tr Attributes Add( onmouseleave ChangeClassName(this + CSS_MAIN_ONMOUSEOUT_CLASSNAME + ) );                     //                         for (int i = ; i < tr Cells Count; i++)                                              TableCell tc = tr Cells[i];                         //tc Style Add( align left );                         if (IsCustomStyle == false)                                                      tc BorderColor = this BorderColor;                             tc BorderStyle = this BorderStyle;                                                      string strBorderWidth = int Parse(this BorderWidth Value ToString()) ToString();                             //应用用户定义样式                         if (IsCustomStyle)                                                      tc CssClass = this CSS_MAIN_ONMOUSEOUT_CLASSNAME;                                                  else                                                      tc CssClass = this CssClass;                                                          if (i == )                                                      if (IsCustomStyle == false)                                                              tc BorderWidth = this BorderWidth;                                 tc Style Add( border Width strBorderWidth + px px + strBorderWidth + px + strBorderWidth + px );                                                          else                                                              tc Style Add( border Right Width px );                                 //固定Border width                                 //tc Style Add( border Width px px px px );                                                              if (this Command != null && this Command Length > )                                                              string strCommand = this Command Trim() Replace( \\ ) Replace( &nbsp; );                                 tc Attributes Add( onclick strCommand);                                     if (IsCustomStyle == false)                                                                      string str = Color FromName( LightGray ) ToString();                                     tc Attributes Add( onmouseenter this style borderStyle= inset ; this parentElement children( ) style borderStyle= inset ; );                                     tc Attributes Add( onmouseleave this style borderStyle= outset ;this parentElement children( ) style borderStyle= outset ; );                                                                  else                                                                      tc Attributes Add( onmouseenter ChangeClassName(this + CSS_MAIN_ONMOUSEOVER_CLASSNAME + ); + ChangeClassName(this parentElement children( ) + CSS_MAIN_ONMOUSEOVER_CLASSNAME + ); );                                     tc Attributes Add( onmouseleave ChangeClassName(this + CSS_MAIN_ONMOUSEOUT_CLASSNAME + ); + ChangeClassName(this parentElement children( ) + CSS_MAIN_ONMOUSEOUT_CLASSNAME + ); );                                                                                               //tc Style Add( border color red );                             //tc Style Add( width ((System Web UI WebControls Image)tc Controls[ ]) Width ToString());                                                  else if (i == )                                                      if (IsCustomStyle == false)                                                              tc BorderWidth = this BorderWidth;                                 tc Style Add( border Width strBorderWidth + px px + strBorderWidth + px px );                                                          else                                                              tc Style Add( border Left Width px );                                 //固定Border width                                 //tc Style Add( border Width px px px px );                                                              if (this Command != null && this Command Length > )                                                              string strCommand = this Command Trim() Replace( \\ ) Replace( &nbsp; );                                 tc Attributes Add( onclick strCommand);                                 if (IsCustomStyle == false)                                                                      tc Attributes Add( onmouseenter this style borderStyle= inset ; this parentElement children( ) style borderStyle= inset ; );                                     tc Attributes Add( onmouseleave this style borderStyle= outset ;this parentElement children( ) style borderStyle= outset ; );                                 //    tc CssClass = mainexit ;                                 //    tc Attributes Add( onmouseenter ChangeClassName(this mainfocus );ChangeClassName(this parentElement children( ) mainfocus ); );                                 //    tc Attributes Add( onmouseleave ChangeClassName(this mainexit );ChangeClassName(this parentElement children( ) mainexit ); );                                                                  else                                                                      tc Attributes Add( onmouseenter ChangeClassName(this + CSS_MAIN_ONMOUSEOVER_CLASSNAME + ); + ChangeClassName(this parentElement children( ) + CSS_MAIN_ONMOUSEOVER_CLASSNAME + ); );                                     tc Attributes Add( onmouseleave ChangeClassName(this + CSS_MAIN_ONMOUSEOUT_CLASSNAME + ); + ChangeClassName(this parentElement children( ) + CSS_MAIN_ONMOUSEOUT_CLASSNAME + ); );                                                                                               if (IsCustomStyle == false)                                                              tc Font MergeWith(this Font);                                                                               else if (i == )                                                      if (IsCustomStyle == false)                                                              tc BorderWidth = this BorderWidth;                                                          else                                                              //固定Border width                                 //tc Style Add( border Width px px px px );                                                          if (IsCustomStyle == true)                                                              tc Attributes Add( onmouseenter ChangeClassName(this + CSS_MAIN_ONMOUSEOVER_CLASSNAME + ); );                                 tc Attributes Add( onmouseleave ChangeClassName(this + CSS_MAIN_ONMOUSEOUT_CLASSNAME + ); );                                                          else                                                              tc Attributes Add( onmouseenter this style borderStyle= inset ; );                                 tc Attributes Add( onmouseleave this style borderStyle= outset ; );                                                          //tc Style Add( border color red );                                                      if (IsCustomStyle == false)                                                      tc ForeColor = this ForeColor;                             tc BackColor = this BackColor;                                                      if (tc ID == tcDownID)                                                      if (this Items Count > )                                                              tc Attributes Add( onclick activeMenu( + this UniqueID + ); );                                                          else                                                              tc Enabled = false;                                 //tc Style Add( display none );                                 if (tc Parent Controls Count >= )                                                                      ((TableCell)tc Parent Controls[ ]) Style Add( border Right Width px );                                                                                                                                                           /// <summary>             /// 创建子控件的层次结构             /// </summary>             protected override void CreateChildControls()                              Table t = new Table();                 tbID = this UniqueID + _table ;                 t ID = tbID;                     TableRow tr = new TableRow();                 t Rows Add(tr);                     TableCell tc_img = new TableCell();                 if (this ClientPath != String Empty || this ImageName != String Empty)                                      string strImageUrl = Path Combine(this ClientPath ToolButtonImages\\\\ + this ImageName);                     System Web UI WebControls Image image = new System Web UI WebControls Image();                     image BorderWidth = Unit Pixel( );                     image ImageUrl = strImageUrl;                     tc_img Controls Add(image);                     tr Cells Add(tc_img);                                      TableCell tc_text = new TableCell();                 tc_text Controls Add(new LiteralControl( &nbsp; + this Text + &nbsp; ));                 tr Cells Add(tc_text);                     TableCell tc_downmenu = new TableCell();                 tcDownID = this UniqueID + tcDown ;                 tc_downmenu ID = tcDownID;                 tc_downmenu Controls Add(new LiteralControl( <font size= >▼</font> ));                 tr Cells Add(tc_downmenu);                     this Controls Add(t);                 base CreateChildControls();                              //protected override void RenderContents(HtmlTextWriter writer)             //             //    base RenderContents(writer);             //                 /// <summary>             /// 注册客户端脚本             /// </summary>             /// <param name= e ></param>             protected override void OnPreRender(EventArgs e)                                  //引用JS文件                 if (!Page ClientScript IsClientScriptBlockRegistered(this GetType() BuildMenu ))                                      //string jsPath = clientPath + /Js/ + jsscript js ;                     Page ClientScript RegisterClientScriptBlock(this GetType() BuildMenu                          <script type=

相关参考

知识大全 ASP.NET 2.0服务器控件开发精要

ASP.NET2.0服务器控件开发精要  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  利用ASP

知识大全 ASP.NET 2.0服务器控件开发的基本概念

ASP.NET2.0服务器控件开发的基本概念  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  利用

知识大全 Asp.NET自定义DataGrid控件

Asp.NET自定义DataGrid控件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  一&nb

知识大全 使用ASP.Net中的自定义控件

使用ASP.Net中的自定义控件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  if((data

知识大全 Asp.net 用户控件添加自定义事件

Asp.net用户控件添加自定义事件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n

知识大全 使用ASP.Net中的自定义控件[4]

使用ASP.Net中的自定义控件[4]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &

知识大全 使用ASP.Net中的自定义控件[5]

使用ASP.Net中的自定义控件[5]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &

知识大全 使用ASP.Net中的自定义控件[2]

使用ASP.Net中的自定义控件[2]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &

知识大全 使用ASP.Net中的自定义控件[7]

使用ASP.Net中的自定义控件[7]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &

知识大全 使用ASP.Net中的自定义控件[3]

使用ASP.Net中的自定义控件[3]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &