【问题标题】:ASP Web Form Control Add With MasterFormClass.cs使用 MasterFormClass.cs 添加 ASP Web 表单控件
【发布时间】:2016-03-03 07:59:24
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net webforms


    【解决方案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="server" 将 div 直接设为服务器控件并设置其可见性。

    【讨论】:

    • 我无法在 aspx 页面中编写代码。有400多个aspx页面。
    • this.Form.Controls.AddAt(0, new Label() { Text = "###", ID = "MyFancyLabel" });
    • 非常好 :) 我正在寻找这个。请写成单独的答案。我确认
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-22
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2012-02-24
      相关资源
      最近更新 更多