ASP Web 表单控件添加 MasterFormClass.cs
ASP Web Form Control Add With MasterFormClass.cs
我用MasterFormClass.cs
添加Body控件
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>
但是...它会添加到页面底部。我想在 body.
下面添加
这一定是我想要的结果:
<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: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- 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);
}
我用MasterFormClass.cs
添加Body控件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>
但是...它会添加到页面底部。我想在 body.
下面添加这一定是我想要的结果:
<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: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- 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);
}