So with the introduction of ASP.NET 2.0, one of the newest and most hyped about feature is Master Pages. With master pages, one can easily create web templates for their website. The basic idea is you create a template known as a master page, and you set certain spots in your template html code as place holders. Then when you create an aspx page you define which is your master page. Inside your aspx page, the placeholder controls will be the only controls in your aspx page, and whatever code goes inside these placeholder controls will show up where they were placed in the master pages. All that is fine and dandy except, you can't use this for the header part of the html, meaning the placeholders can only be placed between the <body></body> tags of the master pages but not the <head></head> . So what happens if you want to add some meta tags, or some link and script tags?? Well so far the only way I discovered this to be possible is to create html controls in your code behind and add them to your Header object. I have included a code snippet below:
HtmlMeta oMeta;
oMeta = new HtmlMeta();
oMeta.Attributes["http-equiv"] = "Content-Type";
oMeta.Attributes["content"] = "text/html; charset=UTF-8";
this.Header.Controls.Add(oMeta);
HtmlGenericControl oScript = new HtmlGenericControl("script");
oScript.Attributes["language"] = "javascript";
oScript.Attributes["type"] = "text/javascript";
oScript.Attributes["src"] = "../Scripts/script.js";
this.Header.Controls.Add(oScript);
So what this code does is that it create a new html meta object, sets the http-equiv attribute to content-type and sets the content to text/html; charset=UTF-8. This informs the browser to read the text as UTF-8 encoded. (this is useful if your website is in a language other than english, and you tell the browser what kind of encoding it is) The code also creates a script tag and adds it to the header, I have not found a HtmlScript control so I used an HtmlGenericControl to build the script tag. This is helpful if you want different pages to have different scripts involved. So this is how you add items between the <head></head> tag in your pages that work off of a master page.
Sunday, January 08, 2006
Subscribe to:
Post Comments (Atom)
1 comment:
hi Avid Coder...
i wanna be a good programmer and need help from people like you. i also have my own blogger but i want to embed it in my own website just like what you did(it may take several days for me to study the code but i am patient because i'm also a karate practitioner for 5 years). i don't know how to do that. can you help me, please...
allenmcnylbert2003@yahoo.com
thanks.
Post a Comment