In previous ASP programs, we often used Response.Write to dynamically output web page header information, but in ASP.NET, the so-called guiding principle of separation of code and UI no longer wants us to do this (of course, if you are willing, you can, this After all, it’s just a suggestion). I’ve seen others use <title ranut="server" id="titleControl">Default Title</title> to set the title, but I don’t think it’s very good because it will generate redundant id=" titleControl" Personally, I feel that it is better to use the Literal control, because it does not produce a redundant character:). Some people also use js to dynamically set the title, but what do you ask the search engine to do? It
is actually very easy to dynamically control the Head information of the Page. Simply watch the demo first: http://www.lvjiyong.com/demo/aspnet/setheader/
The demonstration is very simple. You can download the source code and take a closer look. The annotations are all there.
Literal and PlaceHolder are mainly used.
The method is placed in BasePage. Next time you let WebForm inherit the BasePage class, you can call it. Use Literal to set the Title information. Use PlaceHolder to load the style sheet and script.
Literal allows us to directly set text information. When using it, we first let the program find out whether there is a specified Literal control, and if so, set the Text
/**//// <summary>
/// Set Head information
/// </summary>
/// <param name="_name">Control</param>
/// <param name="_control">Text</param>
private void setHeader(string _control, string _text)
{
Literal obj = this.FindControl(_control) as Literal;
if(obj != null)
{
obj.Text = _text;
}
}
For example, if we set <asp:Literal id="PageTitle" runat="server" /> in the Head area, we can set the title like this. Here we first prepare a method.
/**//// <summary>
///Set web page title
/// </summary>
/// <param name="_title">Webpage title</param>
protected void SetTitle(string _title)
{
setHeader("PageTitle","<title>" + _title + "</title>");
In WebFrom we can set the title like this
this.SetTitle("Lu's Blog");
The methods for Description, Keywords, etc. are the same, but you need to set Meta. I won’t go into this. You can look at the source code.
Now I will talk about the loading of js and css. This time we use the PlaceHolder control. Why use this instead of Literal? Think for yourself
PlaceHolder generally only occupies a position, which facilitates us to dynamically load controls. This makes loading js and css much more convenient. First, we also find the specified PlaceHolder control.
/**//// <summary>
/// Find the PlaceHolder container of LoadHeader
/// </summary>
/// <returns></returns>
private Control findHeader()
{
return this.FindControl("LoadHeader");
}Then add the control dynamically
/**//// <summary>
/// Load the control into the PlaceHolder control
/// </summary>
/// <param name="_obj"></param>
protected void LoadPlaceHolder(HtmlGenericControl _obj)
{
objHeader = findHeader() as Control;
//Load script file
if(objHeader != null)
{
objHeader.Controls.Add(_obj);
}
}To load CSS or JS, we first use HtmlGenericControl to create a child control and then add it to the PlaceHolder control
/**//// <summary>
/// Load the specified style sheet file
/// </summary>
/// <param name="_cssPath">Style sheet file address</param>
protected void LoadCss(string _cssPath)
{
HtmlGenericControl objCss = new HtmlGenericControl("link");
objCss.Attributes["rel"] = "stylesheet";
objCss.Attributes["type"] = "text/css";
objCss.Attributes["href"] = _cssPath;
objCss.Attributes["media"] = "screen";
this.LoadPlaceHolder(objCss);
}
Then when we want to load the style sheet in WebForm, we just need to use
this.LoadCss("Style Sheet Address"); It's the same as loading js. I won't mention it.
The function I wrote in the source code is a little better than what is mentioned here. You can download it and see for yourself.