asp.net2.0自定义空件开发[创建自定义右键popupmenu空件]

(一). 概述

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

        2. 仅当控件支持oncontextmenu事件.

(二). 运行如图

asp.net2.0自定义空件开发[创建自定义右键popupmenu空件]   asp.net2.0自定义空件开发[创建自定义右键popupmenu空件]

(三). 用法

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

   2. 从工具箱拖动一个PopupTraceMenu控件和另一个支持oncontextmenu事件的控件到设计器中,

       比如:

   3. 设置PopupTraceMenur的属性TargetControl=GridView1

   4. F5运行即可

(四). 完整代码

    1. 主控件PopupTraceMenu.cs代码

  1    ///  
  2       ///  Author: ChengKing(ZhengJian)
  3       ///  
  4      [DefaultProperty( " TargetControl " )]
  5      [ToolboxData( " <{0}:PopupTraceMenu runat=server> " )]
  6       // [Designer(typeof(System.ComponentModel.Design.DesignerCollection))]
  7       public   class  PopupTraceMenu : Control, IComponent
  8      {
  9 
 10          [Bindable( true )]
 11          [Category( " Appearance " )]
 12          [DefaultValue( " [PopupTraceMenu \ " PopupTraceMenu11\ " ] " )]
 13          [Localizable( true )]
 14           public   string  Text
 15          {
 16               get
 17              {
 18                  String s  =  (String)ViewState[ " Text " ];
 19                   return  ((s  ==   null ?  String.Empty : s);
 20              }
 21 
 22               set
 23              {
 24                  ViewState[ " Text " =  value;
 25              }
 26          }
 27 
 28          [Bindable( true )]
 29          [Category( " Appearance " )]
 30          [DefaultValue( "" )]
 31          [Description( " 设置此控件的寄主控件 " )]
 32          [TypeConverter( typeof (ControlIDConverter))] 
 33           public   string  TargetControl
 34          {
 35               get
 36              {
 37                  String s  =  (String)ViewState[ " TargetControl " ];
 38                   return  ((s  ==   null ?  String.Empty : s);                                                
 39              }
 40               set
 41              {
 42                  ViewState[ " TargetControl " =  value;
 43              }
 44          }
 45 
 46           protected   override   void  Render(HtmlTextWriter writer)
 47          {
 48               if  (DesignMode)
 49              {
 50                   this .Controls.Clear();                
 51                  LiteralControl lc  =   new  LiteralControl();
 52                  lc.Text  =   this .Text;                
 53                   this .Controls.Add(lc);                
 54              }
 55               base .Render(writer);            
 56          }
 57 
 58           protected   override   void  CreateChildControls()
 59          {            
 60               base .CreateChildControls();
 61          }
 62 
 63           protected   override   void  OnPreRender(EventArgs e)
 64          {
 65 
 66               if  ( ! Page.ClientScript.IsClientScriptBlockRegistered( " BuildMenu " ))
 67              {
 68                  Page.ClientScript.RegisterClientScriptBlock( this .GetType(),  " BuildMenu " ,
 69                       " ");
 89                 strInitScript.Append("   } ");
 90                 strInitScript.Append("");
 91             }
 92             else
 93             {
 94                 strInitScript.Append("  ");
 95                 strInitScript.Append("   alert('Please Set TargetControl Property!'); ");
 96                 strInitScript.Append("");
 97             }
 98             if (!Page.ClientScript.IsStartupScriptRegistered("InitScript"))
 99             {
100                 Page.ClientScript.RegisterStartupScript(this.GetType(), "InitScript",
101                     strInitScript.ToString());
102             }
103            
104             base.OnPreRender(e);
105         }        
106     }

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

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

  cc1:PopupTraceMenu>                      
4  div>

(五). 示例代码下载

       http://files.cnblogs.com/MVP33650/自定义控件开发--[右键菜单].rar

 

 



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1547692


 

(五). 示例代码下载

       http://files.cnblogs.com/MVP33650/自定义控件开发--[右键菜单].rar

 

 



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1547692


 

  cc1:PopupTraceMenu>                      
4  div>

(五). 示例代码下载

       http://files.cnblogs.com/MVP33650/自定义控件开发--[右键菜单].rar

 

 



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1547692


 

(五). 示例代码下载

       http://files.cnblogs.com/MVP33650/自定义控件开发--[右键菜单].rar

你可能感兴趣的:(asp.net)