Dynamically controlling the head information for page pages

Source: Internet
Author: User
Dynamic | control

In the previous ASP program we often use Response.Write Dynamic output header information, but in asp.net this so-called code and UI separation of the guiding ideology no longer want us to do this (of course, if you would like to, this is only a suggestion), I've seen someone use the <title ranut= "server" id= "Titlecontrol" >default title</title> to set the title, but I don't think it's very good because it will produce extra id= " Titlecontrol "personal feeling to use literal control control is better, because it will not produce an extra character:", also someone uses the dynamic setting of JS title, but this you let search engine how to do?

Dynamic Control page head information is actually very simple.
First Look at the demo: http://www.lvjiyong.com/demo/aspnet/setheader/
Demo do very simple, we can get down the source code good good-looking, the annotation inside have

The main use of literal and placeholder
method is placed in the BasePage, the next time you let WebForm inherit the BasePage class, you can call the
Use literal to set title information
Using placeholder to load style sheets and scripts
Literal allows us to directly set the text information, when used we first let the program find out whether there is a specified literal control, if there is set text


/**////<summary>
Set Head information
</summary>
<param name= "_name" > Controls </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, we set up the <asp:literal id= "pagetitle" runat= "Server"/&gt in the head area, so we can set the title
Here's a way to get started.
/**////<summary>
Set Page title
</summary>
<param name= "_title" > page 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");
Description,keywords and so on, but to set meta, this will not speak, you can see the source code

Below I say again JS and CSS load, this time we use is placeholder control, why use this not to follow literal, oneself think
Placeholder generally only occupy position, convenient for us to dynamically load controls, so load JS and CSS is more convenient
First, also find the specified placeholder control
/**////<summary>
Find placeholder containers for Loadheader
</summary>
<returns></returns>
Private Control Findheader ()
{
return this. FindControl ("Loadheader");
Then add the controls dynamically
/**////<summary>
Mount control to placeholder control
</summary>
<param name= "_obj" ></param>
protected void Loadplaceholder (HtmlGenericControl _obj)
{
Objheader = Findheader () as control;

Loading script files
if (Objheader!= null)
{
OBJHEADER.CONTROLS.ADD (_obj);

}
To load CSS or JS we first use HtmlGenericControl to create the 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);


}
So we're in the WebForm. To load a style sheet, use only the
This. LOADCSS ("style sheet address"); Load JS, don't say it.

In the source code I write the function than here said a little bit stronger, we can take a look at their own.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.