Fatih GÜRDAL
Fatih GÜRDAL

Reputation: 1519

ASP Web Form Control Add With MasterFormClass.cs

I add Body Control with MasterFormClass.cs

protected override void OnInit(EventArgs e)
    {
        HtmlGenericControl NewControl = new HtmlGenericControl("div");
        NewControl.ID = "LoadingSpinImage";
        NewControl.Attributes.Add("style", "background-image: url('../../common/images/477.gif'); background-repeat: no-repeat; background-attachment: fixed; background-position: center; width: 100%; height: 98%; position: absolute; top: 1px;     background-color: rgba(255,255,255,0.6);");
        this.Form.Controls.Add(NewControl);
        base.OnInit(e);
    }

Result:

<html>
  <body>
   .... all control or element
   .... all control or element
   .... all control or element
    <div id="LoadingSpinImage" style="width: 100%; height: 98%; position: absolute; top: 1px;  background-image: url('http://preloaders.net/preloaders/495/Spinning%20segments.gif'); background-attachment: fixed; background-color: rgba(255, 255, 255, 0.6); background-position: 50% 50%; background-repeat: no-repeat;"></div>
  </body>
  </html>

But... It adds to the bottom of the page. I want to add just below the body.

This must be the result I wanted :

<html>
  <body>       
    <div id="LoadingSpinImage" style="width: 100%; height: 98%; position: absolute; top: 1px; background-image: url('http://preloaders.net/preloaders/495/Spinning%20segments.gif'); background-attachment: fixed; background-color: rgba(255, 255, 255, 0.6); background-position: 50% 50%; background-repeat: no-repeat;"></div>
   .... all control or element
   .... all control or element
   .... all control or element
  </body>
  </html>

Upvotes: 0

Views: 47

Answers (2)

Fatih G&#220;RDAL
Fatih G&#220;RDAL

Reputation: 1519

I found myself solution :) I had the solution by combining two different topics

1- Response Html Edit

2- First html element replace(form element)

Result:

protected override void Render (HtmlTextWriter writer)
    {

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        HtmlTextWriter tw = new HtmlTextWriter(new System.IO.StringWriter(sb));
        //Render the page to the new HtmlTextWriter which actually writes to the stringbuilder
        base.Render(tw);

        //Get the rendered content
        string sContent = sb.ToString();
        string divStr = "<div id=\"LoadingSpinImage\"></div>"+Environment.NewLine+"<form ";

        //regex with firs replace
        var regex = new System.Text.RegularExpressions.Regex(System.Text.RegularExpressions.Regex.Escape("<form "));
        string newContent = regex.Replace(sContent, divStr, 1);

        //Now output it to the page, if you want
        writer.Write(newContent);

    }

Upvotes: 0

Glia
Glia

Reputation: 381

One way would be to add a asp:PlaceHolder on the desired place of the page and add the control to the placeholder.

<asp:PlaceHolder ID="phLoadingSpinImage" runat="server" />

phLoadingSpinImage.Controls.Add(new Label() { Text = "@@@", ID = "AnotherLabel" });

A even easyer way would be to place the loading div permanently inside the placeholder and set it's visibility:

    <asp:PlaceHolder ID="phLoadingSpinImage" runat="server">
        <div id="LoadingSpinImage" style="width: 100%; height: 98%; position: absolute; top: 1px;  background-image: url('http://preloaders.net/preloaders/495/Spinning%20segments.gif'); background-attachment: fixed; background-color: rgba(255, 255, 255, 0.6); background-position: 50% 50%; background-repeat: no-repeat;"></div>
    </asp:PlaceHolder>

phLoadingSpinImage.Visible = yourCondition;

You also could make the div directly a server control with runat="server" and set it's visibility.

Upvotes: 1

Related Questions