2013-12-20 174 views
0

我們正在使用HtmlTextWriter動態地將腳本標記添加到頁面,這很好用。我們有一些需要添加異步關鍵字,我不知道如何去做。使用HtmlTextWriter將腳本標記添加到腳本標記

我希望標籤看起來像這樣。

<script id="my_script" async type="text/javascript" src="myscript.js"></script> 

構建標籤的方法如下所示。

internal static void RenderJavaScriptInclude(HtmlTextWriter writer, string  filePath, string Id) 
{ 
    writer.AddAttribute(HtmlTextWriterAttribute.Id, Id); 
    writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript"); 
    writer.AddAttribute(HtmlTextWriterAttribute.Src, filePath); 
    writer.RenderBeginTag(HtmlTextWriterTag.Script); 
    writer.RenderEndTag(); 
} 

如何修改以添加「異步」?

許多感謝一如既往

朗達

+0

你需要做什麼異步?你可以將'async'裝飾器添加到'RenderJavaScriptInclude'中,但是我看不到你需要它的地方。 –

+0

我正在整合第三方應用程序,他們要求它完全像這樣在標記中。這是來自整合文件。 <!DOCTYPE HTML \t公共\t ... \t EN」 \t \t \t \t \t \t YourWebsite.com \t \t \t \t \t \t \t ...的\t標準\t文件\t體的\t您\t網站\t ... \t \t – Rhonda

+0

啊,對不對在輸出中。據我所知,格式應該是'async =「async」'這基本上是writer.AddAttribute(「async」,「async」)。只要我可以告訴這將做你想要的(即輸出屬性沒有值。否則,你試過'writer.AddAttribute(「async」,string.Empty)''或'writer.AddAttribute(「async」,空)'? –

回答

0

根據該RenderBeginTag方法的源代碼,具有價值的任何屬性等於null(但不String.Empty)無引號值將被渲染。因此,在致電RenderBeginTag之前致電writer.AddAttribute("async", null);