user control add code to <HEAD>?

  • Thread starter thomasamillergoogle
  • Start date
T

thomasamillergoogle

Is it possible that my user control can add code to the HEAD of the
parent page that hosts the control?
 
K

Karl Seguin

Have you tried?

I know for sure that a custom server control can...

For example:

public class Head : LiteralControl
{
protected override void Render(HtmlTextWriter writer)
{
StringBuilder head = new StringBuilder();
head.Append("<link rel=\"stylesheet\" type=\"text/css\" href=\"");
head.Append(Utility.ApplicationPath);
head.Append("/styles.css\">");
head.Append(Environment.NewLine);
head.Append("<script language=\"javascript\" src=\"");
head.Append(Utility.ApplicationPath);
head.Append("/script.js\"></script>");
base.Text = head.ToString();
base.Render (writer);
}
}

Karl
 
T

thomasamillergoogle

That will only work if the user control is placed inside the HEAD
though, correct?

I am looking for a solution that will FIND the HEAD tag in the parent
control automagically.

Tom
 
K

Karl Seguin

<HEAD id="Head" runat="server">
<title>Test</title>
</HEAD>


user control page_load:
HtmlContainerControl head = (HtmlContainerControl) Page.FindControl("Head");
if (head != null)
{
Literal lit = new Literal();
lit.Text = "<meta something=something />";
head.Controls.Add(lit);
}

Doesn't seem "right" that your user control does this....
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top