• 如何构建自定义控件


    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);
            }

        }

  • 相关阅读:
    Spring总结(一)
    Java中NumberUtil工具类(持续更新中)
    Java中DateUtil工具类(持续更新中)
    解决poi导出报4000最大样式错误:java.lang.IllegalStateException: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook错误
    MySQL 5.7 zip 安装
    Java中String类方法
    Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in class path resource [applicationContext-common.xml]: Cannot resolve reference to bean 'sqlSessionFactory'
    Ajax中async与cache参数
    打印类对象自动调用toString方法
    JSP基本语法小结
  • 原文地址:https://www.cnblogs.com/ahuang1118/p/172569.html
Copyright © 2020-2023  润新知