Asp.net 2.0 自定义控件开发[创建自定义浮动菜单FloadMenu控件][示例代码下载]

(一). 概述

        1. 当鼠标悬浮到控件对象上,  会弹出此自定义控件.

        2. 具有通用性, 只要支持 onmouseenter/onmouseleave 等几个事件方法的目标控件都可以使用此控件.

        3. 此控件与前面自定义控件[右击弹出菜单]控件有些类似,  区别是这个控件客户端JavaScript部分有些麻烦.

(二). 运行如图

  

(三). 用法

   1. 将FloatTraceMenu控件Dll添加引用到Web站点工程中

   2. 从工具箱拖动一个FloatTraceMenu控件和另一个支持onmouseenter/onmouseleave 等事件

       的控件到设计器中, 比如:     1 /// 


  2     /// Author: [ ChengKing(ZhengJian) ] 
  3     /// Blog:   Http://blog.csdn.net/ChengKing
  4     /// Date:   2007/4/3
  5     /// 

  6      [DefaultProperty( " TargetControl " )]
  7      [ToolboxData( " <{0}:FloatTraceMenu runat=server> " )]
  8       // [Designer(typeof(System.ComponentModel.Design.DesignerCollection))]
  9       public   class  FloatTraceMenu : Control, IComponent
 10      {
 11 
 12          [Bindable( true )]
 13          [Category( " Appearance " )]
 14          [DefaultValue( " [FloatTraceMenu / " FloatTraceMenu1/ " ] " )]
 15          [Localizable( true )]
 16           public   string  Text
 17          {
 18               get
 19              {
 20                  String s  =  (String)ViewState[ " Text " ];
 21                   return  ((s  ==   null ?  String.Empty : s);
 22              }
 23 
 24               set
 25              {
 26                  ViewState[ " Text " =  value;
 27              }
 28          }
 29 
 30          [Bindable( true )]
 31          [Category( " Appearance " )]
 32          [DefaultValue( "" )]
 33          [Description( " 设置此控件的寄主控件 " )]
 34          [TypeConverter( typeof (ControlIDConverter))]  
 35           public   string  TargetControl
 36          {
 37               get
 38              {
 39                  String s  =  (String)ViewState[ " TargetControl " ];
 40                   return  ((s  ==   null ?  String.Empty : s);                                                
 41              }
 42               set
 43              {
 44                  ViewState[ " TargetControl " =  value;
 45              }
 46          }
 47 
 48           protected   override   void  Render(HtmlTextWriter writer)
 49          {
 50               if  (DesignMode)
 51              {
 52                   this .Controls.Clear();                
 53                  LiteralControl lc  =   new  LiteralControl();
 54                  lc.Text  =   this .Text;                
 55                   this .Controls.Add(lc);                
 56              }
 57               base .Render(writer);            
 58          }
 59 
 60           protected   override   void  CreateChildControls()
 61          {            
 62               base .CreateChildControls();
 63          }
 64 
 65           protected   override   void  OnPreRender(EventArgs e)
 66          {
 67 
 68               if  ( ! Page.ClientScript.IsClientScriptBlockRegistered( " BuildMenu " ))
 69              {
 70                  Page.ClientScript.RegisterClientScriptBlock( this .GetType(),  " BuildMenu " ,
 71                       " <script type='text/javascript' src='JScript.js'></script> " );
 72              }
 73              
 74               // GridView增加FloatMenu
 75              Control targetControl  =   this .FindControl(TargetControl);
 76 
 77               // 给GridView头增加FloatMenu
 78               // Control targetControl = this.FindControl(TargetControl).Controls[0].Controls[0];
 79 
 80              StringBuilder strInitScript  =   new  StringBuilder();
 81               if  (targetControl  !=   null )
 82              {                
 83                  strInitScript.Append( " <script text/javascript>  " );
 84                  strInitScript.Append( "    writeStyle(); makeMenu(); var obj; " );
 85                  strInitScript.Append( "    if( document.all&&window.print )  " );
 86                  strInitScript.Append( "    {  " );
 87                  strInitScript.Append( "       var objClientId = ' "   +  targetControl.ClientID  +   " '; " );
 88                  strInitScript.Append( "       if(objClientId != null)  " );
 89                  strInitScript.Append( "       { " );
 90                  strInitScript.Append( "          obj = document.getElementById(objClientId); " );
 91                  strInitScript.Append( "          document.onclick = forcehideMenu; " );
 92                   // strInitScript.Append("         obj.onmouseover = showMenu;");
 93                  strInitScript.Append( "          obj.onmouseenter = showMenu; " );
 94                   // strInitScript.Append("         obj.onmouseout = hideMenu;");
 95                  strInitScript.Append( "          obj.onmouseleave = hideMenu; " );
 96                  strInitScript.Append( "          obj.onmousemove = moveMenu; " );
 97                  strInitScript.Append( "          obj.oncontextmenu = showMenu; " );
 98                  strInitScript.Append( "       } " );
 99                  strInitScript.Append( "       else {alert('Please Set TargetControl Property!')} " );
100                  strInitScript.Append( "    }  " );
101                  strInitScript.Append( " </script> " );
102              }
103               else
104              {
105                  strInitScript.Append( " <script text/javascript>  " );
106                  strInitScript.Append( "    alert('Please Set TargetControl Property!');  " );
107                  strInitScript.Append( " </script> " );
108              }
109               if  ( ! Page.ClientScript.IsStartupScriptRegistered( " InitScript " ))
110              {
111                  Page.ClientScript.RegisterStartupScript( this .GetType(),  " InitScript " ,
112                      strInitScript.ToString());
113              }
114             
115               base .OnPreRender(e);
116          }        
117      }

 

   2. 测试页面文件default.aspx代码

1 <div>
2    <asp:GridView ID="GridView1" runat="server"  asp:GridView>  
3    <cc1:FloatTraceMenu ID="FloatTraceMenu1" runat="server" TargetControl= "GridView1">

  cc1:FloatTraceMenu>                      
4  div>

(五). 示例代码下载

        http://www.cnblogs.com/Files/MVP33650/自定义控件开发--[浮动工具条].rar

 

       [扩展]可以自定义(手动编程)浮动菜单的款项

       http://www.cnblogs.com/Files/MVP33650/自定义控件开发--[浮动工具条--可手动编码自定义菜单项]V2.rar

      

(六). 控件开发其它相关文章:

        http://blog.csdn.net/ChengKing/category/288694.aspx

 

 

 

 

 

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值