zoukankan      html  css  js  c++  java
  • 如何构建自定义控件

    public class SubHeader : WebControl   //一定要继承于WebControl且为public类
        {
        
    // The URL to navigate to in case the user is not registered
            private string _register = string.Empty;

            
    public SubHeader()   //在构造函数中定义其大小及样式
            {
                
    // Initialize default values
                this.Width = new Unit(100, UnitType.Percentage);
                
    this.CssClass = "SubHeader";
            }


            
    // Property to allow the user to define the URL for the registration page
            public string RegisterUrl
            
    {
                
    get return _register; }
                
    set { _register = value; }
            }


            
    // This method is called when the control is being built
            
    //重写CreateChildControls方法,从而在此控件内添加其他控件,这样可封装在一起
            protected override void CreateChildControls()
            
    {
                
    // Clear any previously loaded controls一定要先清除自定义控件内的所有控件后,才能添加其他控件。
                this.Controls.Clear();
                Label lbl;
                HyperLink reg 
    = new HyperLink();

                
    if (_register == string.Empty)
                
    {
                    reg.NavigateUrl 
    = Context.Request.ApplicationPath + 
                        Path.AltDirectorySeparatorChar 
    + "Secure" + 
                        Path.AltDirectorySeparatorChar 
    + "NewUser.aspx";
                }
     
                
    else
                
    {
                    reg.NavigateUrl 
    = _register;
                }


                
    if (Context.User.Identity.IsAuthenticated)
                
    {
                    reg.Text 
    = "Edit my profile";
                    reg.ToolTip 
    = "Modify your personal information";
                    HyperLink signout 
    = new HyperLink();
                    signout.NavigateUrl 
    = Context.Request.ApplicationPath + 
                        Path.AltDirectorySeparatorChar 
    + "Logout.aspx";
                    signout.Text 
    = "Logout";
                    signout.ToolTip 
    = "Leave the application";
                    
    this.Controls.Add(new LiteralControl(" | "));
                    
    this.Controls.Add(signout);
                }

                
    else 
                    reg.Text 
    = "Register";

                
    this.Controls.AddAt(0, reg);

                
    // Add it before the logout control if it was added 
                this.Controls.AddAt(0, reg);
                
    this.Controls.Add(new LiteralControl(" - "));

                
    // Add a label with the current data
                lbl = new Label();
                lbl.Text 
    = DateTime.Now.ToLongDateString();
                
    this.Controls.Add(lbl);
            }

        }

  • 相关阅读:
    webpack高级概念,Dllplugin打包性能配置(系列十八)
    webpack高级概念,resolve配置(配置文件格式以及文件路径变量)(系列十七)
    webpack高级概念,eslint配置(系列十六)
    webpack高级概念,解决单页面应用路由问题(vue用脚手架,404找不到页面二)(系列十五)
    webpack高级概念,webpack-dev-server解决单页面应用路由问题(手动搭建webpack,不是用脚手架,404找不到页面,一)(系列十五)
    webpack高级概念,使用 WebpackDevServer 实现请求转发二(系列十四)
    webpack高级概念,使用 WebpackDevServer 实现请求转发一 (系列十四)
    webpack高级概念,typeScript的打包配置(系列十三)
    DevEco Device Tool 2.1 Beta1 的Hi3861在Windows平台的编译体验
    最全HarmonyOS文档和社区资源使用技巧
  • 原文地址:https://www.cnblogs.com/ahuang1118/p/172569.html
Copyright © 2011-2022 走看看