UPDATED RESPONSE (in response to OP's comments)
The whole "are you sure" paradigm can be a little annoying to implement in ASP.NET. Here's an approach I've used to address it. I'm sure there's many other ways to accomplish this, some of which are more elegant. But anyway...
- Put an asp:Hidden field in your form, to indicate "user is sure". Set its default value to false.
- Define your showMsg() javascript function in the .ASPX file directly. It can always be on the page, ready to fire; you don't want or need to conditionally add it via RegisterClientScript. You'll also need to enhance that function to do 2 things if the user confirms "yes I'm sure": a) set your asp:Hidden field's value to true; b) submit the form.
- Implement your btnSubmit_Click() function to check the asp:Hidden value: if that value is true, you know the user has already confirmed the "are you sure" dialog, so you can do your operation (and the hidden value back to false to "reset" it). If the asp:Hidden value is false, trigger your RegisterClientScript to call (not define, but actually call) your showMsg() function.
- Implement Page_LoadComplete to check the hidden value: if it's true, programmatically call btnSubmitClick()
With the above changes made, your page should behave something like this:
- User clicks Submit, page posts back.
- btnSubmit_Click is executed, sees the hidden field is false, and sets the RegisterClientScript to call showMsg().
- Page_LoadComplete sees the hidden field is false, so it does nothing. Postback completes and page is sent back to the browser.
- Browser parses page, comes to showMsg() and runs it. This pops up the are-you-sure dialog.
- User clicks "yes" on the are-you-sure dialog. Your updated showMsg function detects this, sets the hidden field to true and submits the form. Page posts-back.
- Because the form was posted-back but the submit button wasn't actually clicked, your btnSubmit_Click function is NOT called here.
- Page_LoadComplete sees the hidden field is true, so it calls the btnSubmit_Click function.
- btnSubmit_Click sees the hidden field is true, and does the real operation.
The above approach is definitely heavy-handed. If you're willing use ajax to make your decision whether the "are you sure" is necessary, you could move a lot more of this logic to the client side, and save yourself a postback operation.
However, heavy-handed or not, the above approach will work, and will result in relatively easy-to-maintain code, as long as you put in some good comments.
ORIGINAL RESPONSE:
As currently written, showMsg() won't be called until the second time the user clicks the Submit button:
- Button is rendered with no OnClientClick value.
- User clicks the submit button; page posts-back.
- Event handler puts the definition of "function showMsg()" on the page (registerClientScript); it also adds an OnClientClick attribute to the button. Postback completes and page is re-drawn on the browser.
- at this point, showMsg() has not been called by anyone.
- User clicks the submit button.
- Since the button now has an OnClientClick value, this time it should call showMsg.
Other things to consider:
You have the actual javascript function definition in your RegisterClientScript. Why not simply define it on the page (in the .aspx file)? It won't hurt anything to always have it there. It doesn't get fired until it's actually called.
I'm not familiar with the doubled-parens notation you're using: "GetType()()" - if it's not causing a compiler error or not throwing, it may be irrelevant.
Definitely get familiar with Firebug and/or Fiddler, so you can see exactly what rendered HTML is being sent back to the browser. It'll make your web-development debugging MUCH easier.
(arg,context)
from function signature. Hope it helps somebody.