Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-04-24
15
Medium Priority
?
504 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 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

916 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