简体   繁体   English

从ascx.cs动态将文档类型添加到aspx页面

[英]Add doctype to an aspx page dynamically from ascx.cs

Is it possible to add <!DOCTYPE HTML> to an aspx page from the code behind file of a ascx page? 是否可以从ascx页面文件背后的代码中将<!DOCTYPE HTML>添加到aspx页面? Adding doctype to the master page is not an option because it'll wreck the rest of our sharepoint sites. 不能将文档类型添加到母版页,因为它会破坏其余的共享点站点。

I've tried to override the render method: 我试图重写render方法:

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

but apparently it doesn't help. 但显然没有帮助。

For me it worked this way: 对我来说,它是这样工作的:

First I added a literal on top of the page, first line, outside the <Form runat="server" : 首先,我在页面上方第一行的<Form runat="server"之外添加了一个文字:

<asp:Literal runat="server" ID="litHTMLSchema"></asp:Literal>

Then from code-behind: 然后从代码背后:

// HTML 5
litHTMLSchema.Text = @"<!DOCTYPE html>" + Environment.NewLine + @"<html>";

I don't think this is the best approach, but it works without any issues. 我认为这不是最好的方法,但是它没有任何问题。

Since you are using sharepoint, you could create a custom webcontrol in code in a WSP Package Farm Solution. 由于使用的是共享点,因此可以在WSP Package Farm解决方案中的代码中创建自定义Web控件。

  1. Create a class Called DynamicDocTypeControl 创建一个名为DynamicDocTypeControl的类

     public class DynamicDocTypeControl : System.Web.UI.WebControl *(check namespace for typos) { override Render(...) { //add some conditional logic here for your dynamicness... writer.Write("<!DOCTYPE HTML>"); } } 
  2. Add an empty sharepoint element to your project and go to the properties window and use the safe control section in the property window to register your control as a safe control. 在项目中添加一个空的sharepoint元素,然后转到属性窗口,然后使用属性窗口中的“安全控件”部分将控件注册为安全控件。

  3. Build/Package the wsp and deploy it to the farm. 构建/打包wsp并将其部署到服务器场。

Then edit your master page in sharepoint designer and drop your control on it where the doctype should be rendered. 然后在sharepoint设计器中编辑您的母版页,并将您的控件放在应该呈现doctype的位置。

Putting it in the master page won't wreck your sites because you can make your render logic not render anything if it's not on an allowed page. 将其放入母版页不会破坏您的网站,因为如果不在允许的页面上,则可以使呈现逻辑不呈现任何内容。

Just have some code on your aspx page that sets an HttpContext.Current.Item... value that the doctype control looks for to determine if it should render. 只要在您的aspx页面上有一些代码即可设置HttpContext.Current.Item ...值,doctype控件将查找该值以确定是否应呈现。 As long as your aspx page sets the config flag before Render is called it will be there when render fires on the doc type control. 只要您的aspx页面在调用Render之前设置了config标志,当在doc type控件上触发render时,它将存在。

eg 例如

<@ Register TagPrefix="XYZ" Namespace="XYZ.Controls" Assembly="XYZ... (include fully qualified assembly name)" />


<XYZ:DynamicDocTypeControl />

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

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