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.