10 September, 2009

Link tag in Content Page

Recently I faced a problem with attaching JavaScript and Stylesheet files from the Content Page.

Generally all the stylesheets and Java script files are included in the Head section of the Master Page. But sometimes, we may need a javascript/stylesheet file in one particular page. Then it is better to load files only in that page to avoid un-necessary download for all the pages.

When I have this requirement, I started including link tag in the content page. But I came to know that link tag can't work with content pages, when I check my page in the browser. Then I realized that the script and style tags should be used in Content pages. As, Writing long scripts and Styles in the page is not a good practice, I am not convinced with this option and serched for best solution.

There are two solutions for this problem. The best solution is to create a content placeholder in the Head section of the master page and use that placeholder to add extra files in the page wherever needed;

And another solution is to link the css and js files from the code behind. This involves creating a HtmlGenericControl with properties tagname, attributes and innerHtml in the page_Load event of the required page. We can select any of these two to fulfill the need.

--Happy Programming :)