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

    }

  • 相关阅读:
    正则表达式的三种模式【贪婪、勉强、侵占】的分析
    php实用的文件上传类
    php简单实用的验证码生成类
    phpstorm不安装apache就可以本地测试PHP
    Ajax技术——带进度条的文件上传
    Mybatis 多表查询及查询结果映射
    关于textarea包在div的自适应问题
    Luogu P3200 [HNOI2009]有趣的数列
    群&置换群&burnside
    卡特兰树
  • 原文地址:https://www.cnblogs.com/ahuang1118/p/172568.html
Copyright © 2011-2022 走看看