user175084
user175084

Reputation: 4630

confirm message box

I have to show a yes no popup messagebox for a function>

This is what i do for an alert popup>

  Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>alert('File Updated');</script>");

This is what i want to do in the code behind:

if (ID != 0)
{
     Page.ClientScript.RegisterStartupScript(this.GetType(), "Confirm", "<script>confirm('are you sure?');</script>");

    if (yes)
    {
        perform function
    }
    else
    {
        return;
    }

}

The confirm is not working,,, any suggestions on how to do this thanks

Edit Portion:

  1. Navigate to a page
  2. Add values to a textbox
  3. Click "Save" Button to add value to database
  4. Ask the user if he is sure he want to do it 'are you sure'?,, the confirm pop up
  5. Now the above confirm box will only happen if the ID is != 0 or else there is no need for a popup.
  6. if he says yes then add to database and show alert popup that values have been enterd in the DB.
  7. if NO then just dont add to Db and just return.

so i get the confirm box like this.. but how can i get what is selected

string scriptString = "<script language='JavaScript'> ";
            scriptString += "confirm ('Are you sure you want to Close this period.')";
            scriptString += "</script>";
            Response.Write(scriptString);

Upvotes: 1

Views: 15174

Answers (6)

Hans Kesting
Hans Kesting

Reputation: 39264

You can't do it this way. RegisterStartupScript just registers the script to be included when the page is finally rendered. After it is rendered (to HTML) then the html is sent to the browser. So when the user finally sees that popup, your code has long since finished.

EDIT:

See the answer by Mike C.: you need to move that confirm to just before the submit.

Upvotes: 1

Genady Sergeev
Genady Sergeev

Reputation: 1650

Page.ClientScript.RegisterStartupScript will register that script on the client. As far as I can see you are executing the if statement on the server. Could you please specify what confirm is not working mean? Is the alert box displaying but no effect should yes/no is pressed? If so, move the if ... else statement on the client. Anyway, I suggest that you replace RegisterStartupScriptBlock with this code:

ClientScript.RegisterClientScriptBlock
     (this.GetType(), "confirm", "alert('do')", true);

Upvotes: 0

Rob
Rob

Reputation: 45761

It appears that what you're trying to do is (in a simplified scenario):

  1. Have the user navigate to Page.aspx
  2. Check the value of ID (lets assume it's a querystring parameter)
  3. If the value of ID is non-zero, prompt the user to confirm
  4. If they confirm do "something"

The mistake you're making is attempting to handle 2, 3 and 4 alltogether in the code-behind. The script that you emit (by calling RegisterStartupScript) doesn't get executed until the entire page has been rendered back to the user, at which point the code for steps 3 and 4 to check the value of ID and do something will already have been "skipped over"

What you need to do is decide how to separate the work between client-site and server-side as what you're attempting to do just won't work. Without knowing how your page(s) work and where the ID value comes from I can't give a speciic example, but, something like:

  1. Have your page check ID to see if it hits your criteria, if it does, emit the javascript, but with some additional javascript that checks the response to the prompt and causes the page to re-submit but with confirmed=yes added on the querystring
  2. Have your page check the querystring parameter "confirmed" to see if it's yes. If it is, THEN do the work

Upvotes: 1

Mike Cole
Mike Cole

Reputation: 14703

Is there a button you are clicking on to trigger the action? If so, you should add the client events to your web control like so:

<asp:ImageButton runat="server" ID="DeleteUrlImageButton" ImageUrl="~/Images/icon_remove.gif"
  OnClick="DeleteUrlImageButton_Clicked"
  OnClientClick="return confirm('Are you sure you want to delete?');" />

If the user selects yes, the postback happens as usual. If they select no, the even is cancelled and no postback occurs. This, IMO, is the way to handle it because it prevents any extra server activity if they select no.

Upvotes: 6

Karel
Karel

Reputation: 2212

Add a linkbutton.

In the OnClientClick add

javascript:return confirm('Are you sure')

This will not launch the postback if they click no. It will launch the postback if they click yes.

Then in then code behind (OnClick) of the button do your server side processing: (Will only be executed if they click yes)

if (ID != 0)
{
Perform function
}

Upvotes: 2

See the problem here is that, without posting back, you can't get the value of the confirm box. JavaScript is run client-side and until a postback occurs (either via ajax or the regular way), it can't "talk" to your C# file.

What you'll have to do is add a confirm box in JavaScript which, if Yes is clicked, will post back to your Asp.net page and run code either through Ajax or (example) form.submit().

Upvotes: 1

Related Questions