0

I used JavaScipt Alert function in ASP.net4.0 code behind file of content Page

public void Show(string message)
 {

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

When i click on button to get this script call then the alert message came on blank screen then the same screen gets loaded fully.

I want this message after loading the whole page(Msg will get displayed on same page with back controls) and then msg is displayed. Using Master Pages and content Pages..

Please help on

4 Answers 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.

Sign up to request clarification or add additional context in comments.

1 Comment

forgot to include this RegisterStartUpScript but I also used the same but nothing happened..The problem remains same.
1

Setting a timeout period which will allow all the controls in the page to load may also works. See the code below which works for me.

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

PS: Please change the timeout period according to your requirements.

1 Comment

Most reasonable answer I've found so far. Working with ajax toolkit and a tabcontainer often leads to an empty tab when ready is fired. A timeout of 50ms did the trick for me and is almost unnoticeable!
0

codebehind:

  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>

Comments

0

Renderer Blocks!

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

in .cs

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.