zoukankan      html  css  js  c++  java
  • 如何定义其他页面的基类

    public class FriendsBase : System.Web.UI.Page  //必须继承于Page类
        {
            
    protected string HeaderMessage = String.Empty;
            
    protected string HeaderIconImageUrl = String.Empty;

            FriendsFooter _footer;
            FriendsHeader _header;
            SubHeader _subheader;
    //重写OnInit方法,将一些自定义控件及用户控件载入,并都放入0位置
            protected override void OnInit(EventArgs e)
            
    {
                _header 
    = (FriendsHeader) LoadControl
                    (Request.ApplicationPath 
    + Path.AltDirectorySeparatorChar +
                    
    "Controls/FriendsHeader.ascx");
                _footer 
    = (FriendsFooter) LoadControl
                    (Request.ApplicationPath 
    + Path.AltDirectorySeparatorChar +
                    
    "Controls/FriendsFooter.ascx");

                _subheader 
    = new SubHeader();

                
    // Add to the Controls hierarchy to get proper
                
    // event handling, on rendering we position them
                Page.Controls.AddAt(0, _header);
                Page.Controls.AddAt(
    0, _subheader);
                Page.Controls.AddAt(
    0, _footer);
                
    base.OnInit(e);
            }

    //重写Render方法
            protected override void Render(System.Web.UI.HtmlTextWriter writer)
            
    {
                
    //移除页面类所包含的各个层次级别中的控件 Remove the controls from their current place in the hierarchy
                Page.Controls.Remove(_header);
                Page.Controls.Remove(_subheader);
                Page.Controls.Remove(_footer);

                Debug.Assert(
                    Page.Controls[
    1].ToString()=="System.Web.UI.HtmlControls.HtmlForm"
                    
    "Form control not found",
                    
    "Any FriendsReunion page requires that a Form control be " + 
                    
    "located at index 1 of the Page.Controls collection");

                
                
    // 页面Page为索引号为1的控件Get a reference to the form control
                HtmlForm form = (HtmlForm)Page.Controls[1];
                
                
    //在页面上重新分配各种控件 Reposition the controls on the page
                form.Controls.AddAt(0, _header );
                form.Controls.AddAt(
    1, _subheader );
                form.Controls.AddAt(form.Controls.Count, _footer );


                
    //Set current values
                _header.Message = HeaderMessage;
                _header.IconImageUrl 
    = HeaderIconImageUrl;
    //调用基类的Render方法
                base.Render(writer);
            }

    }

  • 相关阅读:
    SpringBoot整合flyway
    JavaFTP递归查询指定目录下的所有目录和文件
    初识网络设备
    Session
    Cookie
    文件下载
    PHP文件上传
    数据库操作函数笔记
    Apache #Tomcat CVE-2020-9484
    红方人员实战手册
  • 原文地址:https://www.cnblogs.com/ahuang1118/p/172568.html
Copyright © 2011-2022 走看看