2012-04-10 73 views
0

Java脚本提示功能我ASP.net4.0代码中使用javascipt的报警功能背后的内容页文件调用时,整个ASP.Net页面获取负载

public void Show(string message) 
{ 

    Page.RegisterClientScript(this.GetType(), "Alert", "<script type=text/javascript>alert('+message+')</script>");   
} 

当我点击链接得到这个脚本调用,然后警报消息出现在黑屏上,然后同样的屏幕被完全加载。

加载整个页面后,我想要这条消息(Msg将显示在具有后控制的同一页上),然后显示msg。 使用母版页和内容页..

回答

1

请帮助,如果你想显示在文档加载时,使用RegisterStartupScript

public void Show(string message) 
{ 
    ClientScriptManager cs = Page.ClientScript; 
    Type myType = this.GetType(); 
    // Check to see if the startup script is already registered. 
    if (!cs.IsStartupScriptRegistered(myType, "AlertScript")) 
    { 
     String script = "alert('" + cleanMessage + "')"; 
     cs.RegisterStartupScript(myType, "AlertScript", script, true); 
    } 
} 

RegisterStartupScript会后,所有的渲染你的脚本元素在页面中,这将确保它在其他DOM元素加载后运行。

+0

忘了包括这个RegisterStartUpScript,但我也使用了相同的,但没有发生任何事情..问题依然存在。 – user1324392 2012-04-10 15:23:23

0

代码隐藏

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Script", "showMessage();", True) 

JS

<script type="text/javascript" charset="utf-8"> 
$(document).ready(function() { 
function showMessage() { 
alert("Hello!"); 
} 
}); 
</script> 
0

渲染块!

<html> 
     <head> 
       // <%= GetScripts() %>  here! 
     </head> 
     <body> 
       // <%= GetScripts() %>  or here! 
     </body> 
    </html> 

中的.cs

public string GetScripts() 
    { 
      string exit="<script type='text/javascript' language='javascript'> $(function(){"; 
      exit+= ""; //here! 
      return exit + "});</script>"; 
    } 
1

设置超时时间,这将使页面中所有的控件负荷可能也适用。请参阅以下适用于我的代码。

private void MessageBox(string Msg) { ClientScript.RegisterStartupScript(this.GetType(), "AlertMsg", "<script language='javascript'> setTimeout(function() { alert('" + Msg + "') }, 500); </script>"); }

PS:请根据您的要求更改超时时间。

+0

迄今为止发现的最合理的答案。 使用ajax工具包和tabcontainer时,准备就绪时通常会导致空标签。 50ms的超时对我来说并没有什么用,而且几乎是不明显的! – DerpyNerd 2016-08-30 06:32:23