关于ASP.NET实现"正在加载..."后页面变形的问题!!!
在网上找到的一段代码,可以实现当页面加载时,提示“正在加载...."的提示,效果已经实现,但是用了以下代码后,发现页面所有的内容都变形了,应该是用了HttpContext.Current.Response.Write()的问题。实现"正在加载...."的代码如下:
public static void initJavascript()
{
HttpContext.Current.Response.Write(" <script language=JavaScript type=text/javascript>");
HttpContext.Current.Response.Write("var t_id = setInterval(animate,20);");
HttpContext.Current.Response.Write("var pos=0;var dir=2;var len=0;");
HttpContext.Current.Response.Write("function animate(){");
HttpContext.Current.Response.Write("var elem = document.getElementById('progress');");
HttpContext.Current.Response.Write("if(elem != null) {");
HttpContext.Current.Response.Write("if (pos==0) len += dir;");
HttpContext.Current.Response.Write("if (len>32 || pos>79) pos += dir;");
HttpContext.Current.Response.Write("if (pos>79) len -= dir;");
HttpContext.Current.Response.Write(" if (pos>79 && len==0) pos=0;");
HttpContext.Current.Response.Write("elem.style.left = pos;");
HttpContext.Current.Response.Write("elem.style.width = len;");
HttpContext.Current.Response.Write("}}");
HttpContext.Current.Response.Write("function remove_loading() {");
HttpContext.Current.Response.Write(" this.clearInterval(t_id);");
HttpContext.Current.Response.Write("var targelem = document.getElementById('loader_container');");
HttpContext.Current.Response.Write("targelem.style.display='none';");
HttpContext.Current.Response.Write("targelem.style.visibility='hidden';");
HttpContext.Current.Response.Write("}");
HttpContext.Current.Response.Write("</script>");
HttpContext.Current.Response.Write("<style>");
HttpContext.Current.Response.Write("#loader_container {text-align:center; position:absolute; top:40%; width:100%; left: 0;}");
HttpContext.Current.Response.Write("#loader {font-family:Tahoma, Helvetica, sans; font-size:11.5px; color:#000000; background-color:#FFFFFF; padding:10px 0 16px 0; margin:0 auto; display:block; width:130px; border:1px solid #5a667b; text-align:left; z-index:2;}");
HttpContext.Current.Response.Write("#progress {height:5px; font-size:1px; width:1px; position:relative; top:1px; left:0px; background-color:#8894a8;}");
HttpContext.Current.Response.Write("#loader_bg {background-color:#e4e7eb; position:relative; top:8px; left:8px; height:7px; width:113px; font-size:1px;}");
HttpContext.Current.Response.Write("</style>");
HttpContext.Current.Response.Write("<div id='loader_container'>");
HttpContext.Current.Response.Write("<div id='loader'>");
HttpContext.Current.Response.Write("<div align='center'>页面正在加载中...</div>");
HttpContext.Current.Response.Write("<div id='loader_bg'><div id='progress'> </div></div>");
HttpContext.Current.Response.Write("</div></div>");
HttpContext.Current.Response.Flush();
}
public static void Hindinit()
{
HttpContext.Current.Response.Write(" <script language=JavaScript type=text/javascript>");
HttpContext.Current.Response.Write(" if( document.getElementById('loader_container')) document.getElementById('loader_container').style.display='none';");
HttpContext.Current.Response.Write("</script>");
HttpContext.Current.Response.Flush();
}
然后在要使用的页面的Page_load中加入以上函数的调用即可实现“正在加载...”的提示,但是所有页面内容全部变形,请高手指教,上面代码如何更改才能使页面不变形,最好帮忙帖出代码。谢谢了!!! --------------------编程问答-------------------- Response.Write()
是在页面最上面写出代码 ,破坏的dom结构 。
这么多JS 。
等高人吧 。 --------------------编程问答--------------------
不要用Response.Write();方法。。
--------------------编程问答-------------------- Page.ClientScript.RegisterStartupScript(page.GetType(), "", sb.ToString());
//先声明个StringBuilder,用StringBuilder的append方法去拼接要输出的字符串
StringBuilder sb = new StringBuilder();
sb.Append("");
//再用这个方法输出JS
Page.ClientScript.RegisterStartupScript(page.GetType(), "", sb.ToString());
用上面这个输出后,页面提示错误:
非静态的字段、方法或属性“System.Web.UI.Page.ClientScript.get”要求对象引用 --------------------编程问答--------------------
根据提示的命名空间做了强制转换
Page page = (Page)System.Web.HttpContext.Current.Handler;
同时在CS类中只需要添加以下两个引用即可:
using System.Web;
using System.Web.UI; --------------------编程问答--------------------
Page.ClientScript.RegisterStartupScript(page.GetType(), "", sb.ToString());
这个代码是放在方法里吗?
方法里return 没?
补充:.NET技术 , ASP.NET