0

我在内容页面文件后面的 ASP.net4.0 代码中使用了 JavaScipt Alert 功能

public void Show(string message)
 {

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

当我单击按钮以获取此脚本调用时,警报消息出现在空白屏幕上,然后同一屏幕被完全加载。

我希望在加载整个页面后显示此消息(消息将显示在带有后退控件的同一页面上),然后显示消息。使用母版页和内容页..

请帮忙

4

4 回答 4

1

If you want to show it when the document is loaded, use 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 will render your script after all the elements in the page, this will ensure that it runs after the other DOM elements are loaded.

于 2012-04-10T15:20:24.070 回答
1

设置允许页面中所有控件加载的超时时间也可能有效。请参阅下面对我有用的代码。

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

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

于 2013-07-02T05:38:11.150 回答
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>
于 2012-04-10T16:23:55.417 回答
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>";
     }
于 2012-09-02T05:56:27.727 回答