Is there a ClientScriptManager.RegisterClientScriptInclude equivalent for CSS

后端 未结 6 1245
温柔的废话
温柔的废话 2021-01-11 10:23

The ClientScriptManager.RegisterClientScriptInclude method allows you to register a JavaScript reference with the Page object (checking for duplicates).

Is there an

6条回答
  •  被撕碎了的回忆
    2021-01-11 10:57

    You can add header links to CSS files in ASP.Net codebehind classes like this:

    HtmlLink link = new HtmlLink();
    link.Href = "Cases/EditStyles.css";
    link.Attributes.Add("type", "text/css");
    link.Attributes.Add("rel", "stylesheet");
    this.Header.Controls.Add(link);
    

    You can iterate through the header controls beforehand to see if it is already there. The example shown is from a Page_Load in one of my projects and is inside a conditional expression that only adds the EditStyles.css if the page is supposed to be in "Edit" mode.

    For ClientScriptManager.RegisterClientScriptBlock and ClientScriptManager.RegisterClientScriptResource, they have equivalent functions for checking if they've already been registered (e.g., IsClientScriptrResourceRegistered).

提交回复
热议问题