Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

Add a <div> after the <body> html tag

Hello Experts,
I need to get the body tag and add a div right after it. I cannot use Jquery.doc to do this because I need to do this before the page is rendered. I have tried to do this in page_load and page_pre_render.  The code is here. I know the first line is wrong.

          Dim ctrl As Control = Me.FindControl("body")
            Dim ctrlDiv As HtmlControl = New HtmlGenericControl("div")
            ctrlDiv.Attributes.Add("class", "loader")
            ctrl.Controls.Add(ctrlDiv)
0
bill_home
Asked:
bill_home
  • 7
  • 5
  • 3
2 Solutions
 
käµfm³d 👽Commented:
Why are you adding content after the <body> tag? Why wouldn't you add this content as the last item within the <body>?
0
 
bill_homeAuthor Commented:
Hello thanks for the response

Isn't the page rendered at that point?
0
 
bill_homeAuthor Commented:
Also Kaufmed if you can supply code that you think will work I will try it..

thanks
BIll
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
käµfm³d 👽Commented:
If you're on the server, no. The page isn't rendered until the client gets it. The HTML isn't sent out of the ASP.NET pipeline until the end of the page life cycle.

It's been a while since I've done WebForms, but try:

Dim ctrlDiv As HtmlControl = New HtmlGenericControl("div")

ctrlDiv.Attributes.Add("class", "loader")

Me.Controls.Add(ctrlDiv)

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Using AddAt rather than Add should do what you want:
        Dim ctrl As Control = Me.FindControl("body")

        Dim ctrlDiv As HtmlControl = New HtmlGenericControl("div")
        ctrlDiv.Attributes.Add("class", "loader")

        ctrl.Controls.AddAt(0, ctrlDiv)

Open in new window

0
 
bill_homeAuthor Commented:
Hello Experts.

I can't use Dim ctrl As Control = Me.FindControl("body")
it does not find the control.  I tried kaufmeds and it did add the div. I will try your AddAt to see how that works..

Thanks ALL

I will be here a while so I will let you know ASAP
0
 
Carl TawnSystems and Integration DeveloperCommented:
In order for the FindControl to work you will need to give your body element ID and runat attributes (based on your code I assumed you had already done this).

Also, if you are using master pages, then you would need to change:
Dim ctrl As Control = Me.FindControl("body")

Open in new window

To:
Dim ctrl As Control = Master.FindControl("body")

Open in new window

0
 
bill_homeAuthor Commented:
One more thing this is what is rendered.. Is it ok ? Meaning the DOCTYPE is the second element of the page.  Otherwise this is working great.

<div class="loader"></div>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
0
 
Carl TawnSystems and Integration DeveloperCommented:
The DOCTYPE should be at the very top of the page, outside of any other elements. It shouldn't be inside the BODY tag.
0
 
bill_homeAuthor Commented:
Hello Carl,
It is not in the body that is the top of the page. This code put the div as the first element on the page. I am assuming it is ok?...no?

 ctrl.Controls.AddAt(0, ctrlDiv)
0
 
Carl TawnSystems and Integration DeveloperCommented:
Is the control with the ID of "body" actually the BODY element? Can you post the markup for the page where the BODY element lives?
0
 
bill_homeAuthor Commented:
when I view the frame source the body does not have an id

<body>

when I view page source it does

<body id="mainBody">
    <form name="form1" method="post" action="default.aspx" id="form1">
<div>
0
 
Carl TawnSystems and Integration DeveloperCommented:
What do you mean by "frame source"? Are you using frames in your layout? If you are then each frame will hold a different page. You need to find the body relative to the code where you are trying to insert the <div />.

But regardless of that the BODY element needs an ID and a runat="server" attribute in order for you to be able to access it in code.

i.e.
<body id="mainBody" runat="server">

Open in new window

Would be accessed with:
'// if in the page itself
Dim ctrl As Control = Me.FindControl("mainBody")

'// if you have a master page
Dim ctrl As Control = Master.FindControl("body")

Open in new window

0
 
käµfm³d 👽Commented:
I tried kaufmeds and it did add the div.
I just tested my code suggestion out, and I agree that it does add it; however, it added the <div> outside of the </html> tag, which is really not valid XHTML. If you don't want to go CarlTawn's route, then my only other suggestion would be to put a placeholder in your master page (or in the page itself if you're not using master pages). Then you would have a control with which you could interact programmatically without having to call FindControl. If a placeholder has no content, nothing is rendered for it.

e.g.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication21._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

    <asp:Label ID="Label1" runat="server" Text="This is a label"></asp:Label>
    <asp:ListBox ID="ListBox1" runat="server">
        <asp:ListItem>Some Item</asp:ListItem>
        <asp:ListItem>Another Item</asp:ListItem>
    </asp:ListBox>
    <asp:BulletedList ID="BulletedList1" runat="server">
        <asp:ListItem>An Item</asp:ListItem>
        <asp:ListItem>Yet another Item</asp:ListItem>
    </asp:BulletedList>
    <asp:CheckBox ID="CheckBox1" runat="server" Text="A Checkbox" />
    <asp:Button ID="Button1" runat="server" Text="Submit" />
    <asp:PlaceHolder runat="server" ID="extra_stuff" />
</asp:Content>

Open in new window


Since placeholder has an ID, you can access it in code:
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace WebApplication21
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                HtmlGenericControl ctrlDiv = new HtmlGenericControl("div");

                ctrlDiv.Attributes.Add("class", "loader");
                ctrlDiv.InnerText = "hello World!";
                ctrlDiv.Visible = true;

                this.extra_stuff.Controls.Add(ctrlDiv);
            }
        }
    }
}

Open in new window

0
 
bill_homeAuthor Commented:
very helpful and did not leave me hanging.  I could only choose one best solution but I am going to use a combination of both experts input.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now