ASP Web 窗体控件添加与 MasterFormClass.cs

本文关键字:MasterFormClass cs 添加 控件 Web 窗体 ASP | 更新日期: 2023-09-27 18:37:18

我用MasterFormClass添加了Body Control.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);
    }

结果:

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

ASP Web 窗体控件添加与 MasterFormClass.cs

一种方法是在页面的所需位置添加一个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="server" 直接使div 成为服务器控件并设置其可见性。

我找到了自己的解决方案:)我通过结合两个不同的主题找到了解决方案

1-响应HTML编辑

2-第一个html元素替换(表单元素)

结果:

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