21

I am trying to fire a pop up as shown below, but it is not working. Please help

public void btnSubmit_Click(Object o, EventArgs e)
{
    if (checkFileExists(Convert.ToString(fileInfo)))
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "<script type=\"text/javascript\"  language=\"javascript\">function showMsg(){return confirm(\"This image name already exists, do you want to replace it?\");}</script>", true);
        btnSubmit.OnClientClick = "return showMsg()";
    }
    if (something else)
    {
        // It does whatever is here but never pops the question above
    }
}

and on the button I have

 <asp:Button class="Button" ID="btnSubmit" CausesValidation="True" Text="SUBMIT" runat="server"
             OnClick="btnSubmit_Click"></asp:Button>
1
  • In my case it was not working because the function prototype in my script was wrong missing the (arg,context) from function signature. Hope it helps somebody.
    – TheTechGuy
    Commented Jan 29, 2014 at 16:07

7 Answers 7

42

Another reason for Page.ClientScript.RegisterClientScriptBlock not working is when the page does not have a server side form e.g. <form id="form1" runat="server">... </form>

1
  • that actually worked for me... just runat="server"
    – kplshrm7
    Commented Sep 18, 2023 at 12:29
19

The last parameter you're sending to RegisterClientScriptBlock is true, which tells the method to wrap your script in a <script> block, but you're already doing that. It's probably not working, because it's rendering invalid (i.e. nested) script tags.

2
  • I changed it to false, but it is still ignoring it, it does not pop up. I even took out the if condition to force it to run but nothing, it just continues with the "Submit" and finishes.. never fires the btnSubmit.OnClientClick = "return ShoeMsg()";
    – user710502
    Commented Jun 30, 2011 at 17:43
  • When you view your page's source, do you see the <script> block? Commented Jun 30, 2011 at 18:06
5

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:

  1. User clicks Submit, page posts back.
  2. btnSubmit_Click is executed, sees the hidden field is false, and sets the RegisterClientScript to call showMsg().
  3. Page_LoadComplete sees the hidden field is false, so it does nothing. Postback completes and page is sent back to the browser.
  4. Browser parses page, comes to showMsg() and runs it. This pops up the are-you-sure dialog.
  5. 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.
  6. Because the form was posted-back but the submit button wasn't actually clicked, your btnSubmit_Click function is NOT called here.
  7. Page_LoadComplete sees the hidden field is true, so it calls the btnSubmit_Click function.
  8. 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:

  1. Button is rendered with no OnClientClick value.
  2. User clicks the submit button; page posts-back.
  3. 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.
  4. at this point, showMsg() has not been called by anyone.
  5. User clicks the submit button.
  6. 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.

2
  • The GetType()()is a typo when I posted here. I had it with a OnClientClick in the .aspx, but it would give me a JScript Object Expected error, and I imagined it is because it could not find the method.. it seems like it first goes for the Javascript and then the c# code.. the problem is that I need to evaluate a few things first in c# to decide whether or not to fire the javascript... I dont know what to do :(
    – user710502
    Commented Jun 30, 2011 at 18:13
  • Edited my answer to give you some more detail and a possible approach to the whole thing. I'm not saying its the best approach, but it's only a couple small steps away from what you've been doing so far.
    – mikemanne
    Commented Jun 30, 2011 at 19:45
1

I understand your problem and i suggest you do the following.

1- keep your btSubmit but in your

public void btnSubmit_Click(Object o, EventArgs e)
{
   if (checkFileExists(Convert.ToString(fileInfo)))
   {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "<script type=\"text/javascript\"  language=\"javascript\">function showMsg(){return confirm(\"This image name already exists, do you want to replace it?\");}</script>", true);
        btnSubmit.OnClientClick = "return showMsg()";
   }    
}

delete the if(something else).

2- Use Dialog to create a dialog similar to a javascript confirm but with a asp.net button with its own click event.

3- change your

return confirm(\"This image name already exists, do you want to replace it?\"); 

in the RegisterClientScriptBlock of your first event with the logic of the dialog of jquery.

4- In the new event of your new button add your extra logic:

if (something else)
{
    // you´ll know the user click your confirm button already!!!
}

I hope this helps you.

4
  • One thing to remember with jQuery dialog: when you initialize the dialog, jQuery renders the new tags at the end of the DOM. This is outside of the <form></form> tags. That means, any asp:Field (or similar) elements contained in the dialog will NOT have their values reflected back into the viewstate. This probably doesn't matter for a submit button, but I lost a lot of hours one day trying to figure out why the text in my asp:Field on my dialog was never in the viewstate on postback! :)
    – mikemanne
    Commented Jun 30, 2011 at 19:55
  • Upvoted this answer because I like the 2-different-buttons approach better than the hidden-field-approach I outlined in my answer!
    – mikemanne
    Commented Jun 30, 2011 at 20:02
  • This SO discussion shows an excellent way to make sure jQueryUI elements (dialogs at least) are rendered within the FORM tags, so submit buttons and ViewState-enabled data values work correctly: stackoverflow.com/questions/757232/…
    – mikemanne
    Commented Jul 1, 2011 at 18:45
  • Wouldn't this cause a postback?
    – TheTechGuy
    Commented Jan 28, 2014 at 13:13
0

FishBasketGordo thanks for pointing out the difference! Was a great help!

Page.ClientScript.RegisterClientScriptBlock did not fire until I made the change you pointed out ie removed the tags and added 'true' as the fourth parameter. It may be a good idea to check that no other script block reverses the changes effected by the the one that's not firing and also that the script block has a unique name.

-1

I had the same problem this morning and it only worked this way :

Response.Write("<script type='text/javascript'>alert('" + AlerteMsg + "');</script>");

in your case you can write whatever javascript you wants !

-1

I had the same problem this morning and it only worked this way :

Response.Write("alert('" + AlerteMsg + "');");

in your case you can write whatever javascript you wants !

from above code.. now i need to press 2 times back button..then it goes back.. any solution

Not the answer you're looking for? Browse other questions tagged or ask your own question.