ASP Web窗体控件添加MasterFormClass.cs

时间:2016-03-03 07:59:24

标签: c# asp.net webforms

我使用MasterFormClass.cs

添加Body Control
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);
    }

结果:

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

但是......它增加了页面的底部。我想在身体下方添加。

这一定是我想要的结果:

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

2 个答案:

答案 0 :(得分:1)

一种方法是在页面的所需位置添加一个asp:PlaceHolder,并将控件添加到占位符。

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

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

更简单的方法是将加载div永久放置在占位符内并设置其可见性:

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

您还可以使用runat =&#34; server&#34;直接将div设为服务器控件。并设定它的可见性。

答案 1 :(得分:0)

我发现自己解决方案:)我通过结合两个不同的主题来获得解决方案

1- Response Html Edit

2- First html element replace(form element)

结果:

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);

    }