0

I have the following code behind -

int p = 0;

        try
        {

            p = System.Convert.ToInt16(txt7.Text);
        }
        catch
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "showMyMessage", "ShowMessage('Value must be numerical');", true);
        }

And the function -

 <script>

  function ShowMessage(message) { alert(message); }

</script>

On debugging it goes into the catch however the pop up is not firing in the front end, what is it I am missing?

3
  • By On debugging it goes into the catch however the pop up is not firing in the front end, I hope you're not expecting the popup to appear as soon as you debug yor way over the RegisterStartupScript line? Commented Feb 28, 2013 at 11:44
  • no it is missing this on normal running as well, i have no idea why its not working. Commented Feb 28, 2013 at 11:46
  • Fair enough... I've seen too many people who expect this kind of thing to immediately show up on the client ^^ If you load the page and look at the source, can you see any references to ShowMessage? A definition, or a call to it? Commented Feb 28, 2013 at 11:50

3 Answers 3

1

Place your script at the top of the page (inside your body tag or head tag) as that could be the reason for it.. The startup scrip call should be below your mentioned script. and for best practice please write it like this

<script type="text/javascript">

  function ShowMessage(message)
   {
        alert(message); 
   }    
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I'm pretty sure RegisterStartupScript renders the script at the very end of the page anyway: The script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised.
1

If you are using UpdatePanels, then you should instead use ScriptManager.RegisterStartupScript

Comments

0

Add type of script and place script at the top..

 <script type="text/javascript">

 funcion ShowMessage(message)
 {
  alert(message);
 }

</script>

2 Comments

I don't know but I'm guessing that you haven't answered anything, just rewrote the question with a type attribute.
@happygilmore No..I thought adding type="text/javascript" will solve the problem.Thats why i added that...

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.