自定义并美化Header部分的内容输出

来源:互联网 发布:笔袋淘宝 编辑:程序博客网 时间:2024/05/22 08:10

自定义Header部分的内容输出,并要兼顾代码美化,和任意内容块的输出。

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />            //【位置0 】
<title>无标题文档</title>                                                                                            //【位置1】

..... 

</head>

 

protected void Page_Init(objectsender,  EventArgse)
{
        //Title设置        
        this.Header.Title = base.Title;
        

        //Header设置         
        Literal lit = new Literal();
        StringBuilder sb = new StringBuilder();       
        sb.Append("\r\n");
       
        //Meta设置
        sb.AppendLine("<meta name=\"Keywords\" content=\"" +base.Keywords+ "\" />");
        sb.AppendLine("<meta name=\"Description\" content=\"" +base.Description+ "\" />");
        sb.Append("\r\n");
       
        //CSS设置
        sb.AppendLine("<link type=\"text/css\" rel=\"stylesheet\" href=\"/CSS/main.css\" />");
        sb.Append("\r\n");
       
        //JS设置
        sb.AppendLine("<script language=\"javascript\" type=\"text/javascript\" src=\"/JS/jquery-1.4.2.min.js\"></script>");
        sb.Append("\r\n");
       
        //自定义JS块输出 : IE6 PNG背景图片支持
        sb.AppendLine("<!--[if IE6]>");
        sb.AppendLine("<script  type=\"text/javascript\"  src=\"/JS/DD_belatedPNG-min.js\" ></script>");
        sb.AppendLine("<script>");
        sb.AppendLine("  DD_belatedPNG.fix(\"#layLogo a img, .busPic img, .Floor1 li, .Floor2 li\");");
        sb.AppendLine("</script>");
       
        //自定义JS块输出 :IE6 防止图片背景抖动
        sb.AppendLine("<script type=\"text/javascript\">");
        sb.AppendLine("if((window.navigator.appName.toUpperCase().indexOf(\"MICROSOFT\")>=0)&&(document.execCommand))");
        sb.AppendLine("try{document.execCommand(\"BackgroundImageCache\", false, true);}catch(e){}");
        sb.AppendLine("</script>");
        sb.AppendLine("<![endif]-->");
       
        lit.Text = sb.ToString();
       
        this.Header.Controls.AddAt(2,  lit);        //【位置:2】   

        //注意,由于ASPX页面的<head>中前两个位置已经被占了,所以新添加的位置从2开始 (起始位置为0)
}   

 

 

==================================  

另一种方法【推荐】

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题文档</title>       

   ... ...  

</head>

 

 

protected void Page_Init(objectsender, EventArgs e)
{
        //页面编码设置
        this.Header.Controls.AddAt(0, new Literal       //将页面编码固定在位置0
        {
            Text = "\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\r\n"     //为了美观,需要前后加上换行符
        });
       
        //标题设置
        this.Header.Title = "网页标题";                            //ASPX页面的默认<title></title> 自然变成位置1
          
        
        //Meta设置
        StringBuilder sb = new StringBuilder();    
        sb.Append("\r\n");
        sb.AppendLine("<meta name=\"Keywords\" content=\"" + base.Keywords + "\" />");
        sb.AppendLine("<meta name=\"Description\" content=\"" + base.Description + "\" />");
               ...... .....       
        
        Literal headHtml = new Literal();
        headHtml.Text = sb.ToString();  
       
        this.Header.Controls.AddAt(2,  headHtml );      //将需要加入的代码固定在位置2
               
}