ddevlin1
ddevlin1

Reputation: 45

Response.write to <head>

Hopefully this won't be a difficult question for someone to answer, but I am having a lot of trouble finding the solution online. I am trying to add some HTML to my asp.net page from the code behind (It's VB.net). I would like to add the HTML into the head section of my page but can only add to the body currently.

Upvotes: 0

Views: 1672

Answers (4)

Chesare
Chesare

Reputation: 383

I made this way, and it worked:

on the .aspx file:

... <% Response.Write(GetDisclosureText()); %> ...

on the aspx.cs file:

    protected string GetDisclosureText()
    {
        string disclosure = ""; 
        // ...Apply  custom logic ...
        if (!string.IsNullOrEmpty(disclosure))
        {
            return disclosure;
        }
        return "Error getting Disclosure Text";
    }

Note the only difference is that I call Response.Write, not just the function.

Upvotes: 0

Shyju
Shyju

Reputation: 218732

Have runat attribute on your head element and you will be able to access it

<head id="someHead" runat="server">

</head>

Now in your codebehind, you can set it like

 someHead.InnerHtml="<script src='somelibrary.js' ></script>";

Upvotes: 0

Stokedout
Stokedout

Reputation: 11055

You could try creating a property in your code behind and add your html in the Page_Load method:

Public MyHtml As String

then in the head section of your HTML just use the literal notation:

<%= MyHtml %>

Upvotes: 1

Colin Mackay
Colin Mackay

Reputation: 19175

You can put code in the head, just like the body. For example:

<%= CallAMethodThatReturnsAStringOfHtml() %>

Upvotes: 1

Related Questions