简体   繁体   English

动态更改aspx页面的文档类型

[英]Change the doctype of an aspx page dynamically

We use master pages in our web application, the doctype is defined in the master page. 我们在Web应用程序中使用母版页,文档类型在母版页中定义。

On one of the pages I need to change the doctype, or else a third-party control renders incorrectly. 在其中一个页面上,我需要更改doctype,否则第三方控件将呈现错误。

How can I change the doctype of only that certain page without affecting the rest of the pages? 如何仅更改特定页面的文档类型而不影响其余页面?

到目前为止,最简单的方法是制作母版页的另一个副本,在其中更改doctype并使该页使用新的母版。

This works too.... 这也可以...

protected override void Render(HtmlTextWriter writer)
{
        StringBuilder sb = new StringBuilder("<!DOCTYPE HTML>" + Environment.NewLine);
        HtmlTextWriter textWriter = new HtmlTextWriter(new StringWriter(sb));
        base.Render(textWriter);
        writer.Write(sb.ToString());
    }

I don't know if it would work but 我不知道是否行得通

You can reset the content type with 您可以使用

Response.Clear();
Response.ContentType = "text/html";

Then write your doctype type 然后写你的doctype类型

Response.Write(<new doc-type>);

But you will also loose all meta headers and such, you're probably better off with the other solution provided by Chris Lively... 但是您还将丢失所有元头,因此,最好使用Chris Lively提供的其他解决方案。

Have you used an ASP Literal control? 您是否使用过ASP Literal控件?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:literal runat="server" id="docType"></asp:literal>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

Then in the Page_Load, you could: 然后在Page_Load中,您可以:

this.docType.Text = {your doctype-string here};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM