Solved

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

Posted on 2014-04-24
15
474 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
  • 7
  • 5
  • 3
15 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Hello thanks for the response

Isn't the page rendered at that point?
0
 

Author Comment

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

thanks
BIll
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:bill_home
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 74

Assisted Solution

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now