Solved

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

Posted on 2014-04-24
15
486 Views
Last Modified: 2014-04-25
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
Comment
Question by:bill_home
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
15 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40021307
Why are you adding content after the <body> tag? Why wouldn't you add this content as the last item within the <body>?
0
 

Author Comment

by:bill_home
ID: 40021324
Hello thanks for the response

Isn't the page rendered at that point?
0
 

Author Comment

by:bill_home
ID: 40021334
Also Kaufmed if you can supply code that you think will work I will try it..

thanks
BIll
0
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40021337
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40021345
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
 

Author Comment

by:bill_home
ID: 40021399
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40022051
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
 

Author Comment

by:bill_home
ID: 40022437
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40022440
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
 

Author Comment

by:bill_home
ID: 40022447
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40022469
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
 

Author Comment

by:bill_home
ID: 40022574
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
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 40022615
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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 40022709
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
 

Author Closing Comment

by:bill_home
ID: 40022789
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

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question