How to add script after dynamic title tag

I would like to add a literal dynamically on the page, but immediately after </head>

and before <body>.

. How can I find this section programmatically and add a literal dynamically? So basically the page will look like this:

<head>
</head>
<LITERAL HERE>
<body>
</body>

      

I've seen how to find the title tag somehow, but I don't know how to tell it to add a literal right after it is closed and before the body. (This is for the Bing Ad Center tracking code as they suggest hosting it). But it should be dynamic, because we won't always add tracking every time a user arrives on this page, but only depending on certain cases.

thank

+3


source to share


2 answers


You can use delimiters to insert line content directly into your page.

<head runat="server">
    <title></title>
</head>
<%= TrackingString %>
<body>
</body>

      

Make sure that this line is declared globally on the page that links to it, and when you manipulate this line on the page, it will be written to the page as text and parsed as HTML:



public partial class Default : System.Web.UI.Page
{
    public string TrackingString;

    protected void Page_Load(object sender, EventArgs e)
    {
        TrackingString = "<YourHtmlTag></YourHtmlTag>";
    }
}

      

In cases where you don't want to track the user, just set the string to String.Empty or "" and nothing will be written to HTML.

+3


source


You can use literal control and bind text in page load event

<head>
    <asp:Literal ID="literal1" runat="server"></asp:Literal>
    <title></title>
</head>

      



FROM#

protected void Page_Load(object sender, EventArgs e)
{
    if(condition)
    {
      literal1.Text = "html or script tag";
    }
}

      

0


source







All Articles