Asp. NET page Head area dynamic settings full raiders

Source: Internet
Author: User
Asp.net| News | introduction | page

Page Head area, can not use ordinary controls, but it is sometimes very important, such as title, keywords, description, may be different from each page, then how to dynamically set them according to the content?

Method 1:asp can do, asp.net of course can do, as long as you put the whole page with Response.Write () write out, nothing can not be customized, of course, can also "<%= a member%>." But it is clear that this will not be a feature of. Net.

Method 2: Use ASP.net's unique properties: runat= "Server", set an ID on the title, and then make it a server variable, so you can set its text. But this out of the HTML inside also will have ID, look really uncomfortable.

Method 3: Using the literal control, the front desk:

<HEAD>
<asp:literal id= "Lt_title" runat= "Server"/>
<asp:literal id= "lt_keywords" runat= "Server"/>
<asp:literal id= "LT_DESCRI" runat= "Server"/>
</HEAD>

Background:

private void Page_Load (object sender, System.EventArgs e)
{
Lt_title. Text = "<title> title </title>";
Lt_keywords. Text = "<meta name=\" keywords\ "content=\" keyword \ ">";
Lt_descri. Text = "<meta name=\" description\ "content=\" description \ ">";
}

This is basically the perfect.

Further, my page uses a number of user controls, and these user controls have layers, and the decision page title may be determined by the child user control in a user control, and the user control's nesting level is not fixed. So how do you want to dynamically set it?

First, make a base class for the user control and add 1 public methods to it:

public void Settitle (string title)
{
Setliteraltext ("Lt_title", String. Format ("<title>{0}</title>", title));
Add one more private method:
private void Setliteraltext (string id, string text)
{
Literal lt = null;
Control CTRL = this;
Todo
{
CTRL = Ctrl. Parent;
}while (CTRL!= NULL && CTRL. GetType (). FullName!= "System.Web.UI.HtmlControls.HtmlForm");

if (ctrl!= NULL)
{
lt = Ctrl. FindControl (ID) as Literal;
if (LT!= null)
Lt. Text = text;
}
}

This way, your user control simply needs to inherit from the base class, and then call base when you want to set the title of the page. Settitle ("title"), the simple completion of the task. For other tags in the head area, the principle is the same as setting title.



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.