confirm message box

允我心安 提交于 2019-12-11 07:35:16

问题


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);

回答1:


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.




回答2:


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
}



回答3:


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().




回答4:


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



回答5:


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.




回答6:


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);


来源:https://stackoverflow.com/questions/4933969/confirm-message-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!