最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Confirm message in ASP.NET - Stack Overflow

programmeradmin0浏览0评论

I wrote this statment in my code:

Response.Write("<script language=javascript>confirm('The system not allow negative inventory,continue?');</script>");

how can I handel if the user clicked "Ok" or "Cancel" button?

I wrote this statment in my code:

Response.Write("<script language=javascript>confirm('The system not allow negative inventory,continue?');</script>");

how can I handel if the user clicked "Ok" or "Cancel" button?

Share edited Sep 30, 2009 at 19:40 Chris Conway 16.5k24 gold badges98 silver badges113 bronze badges asked Jul 13, 2009 at 8:35 WafaaWafaa
Add a ment  | 

4 Answers 4

Reset to default 2

You should put this confirm to your submit button like that :

btnSubmit.Attributes["onclick"] += 
    "return confirm('The system not allow negative inventory,continue?');"

If user click cancel, your page won't be postback.

But if you ask you can determine user's action at server side, the answer is no, not directly. You should add some trick, to get the user's action. Maybe you should set the user's action into a hidden field and at server side get this value and continue.

Restricting my answer to Javascript and not how the result of your code would interact with the flow of the page, the Javascript confirm method returns the value of the user's selected option:

var result = confirm('The system does not allow negative inventory. Continue?');
if (result == true)
  // The user wants to continue. Proceed accordingly.
else
  // The user does not want to continue.

You can use the value to branch your logic accordingly.

That code will execute on the client side. To know if the user clicked OK or cancel on the server, you will need to make your script send a request back to the server. You can't handle the dialog in the same request, because your code will have ended before the user sees anything in their browser.

You could output JavaScript like this:

location.href = 'Handler.aspx?confirmed=' + confirm('Do you want to do X?');

This will send the browser to either Handler.aspx?confirmed=true or Handler.aspx?confirmed=false.

On the onClientClick tag (in the aspx file) u can write

confirm('your message');

and onClick tag reference it to function in code-behind where you can write the function which will only be executed on OK click of the messagebox.

Using a javascript confirm function it is possible to allow a user to confirm or canel a button click. If the user presses yes on the message/alert box the button click even will be triggered on the server. If the no button is click no event will be triggered. This is especially useful with buttons used for deleting.

button.Attributes("onclick") = "javascript:return " & _
                 "confirm('Are You Sure you want to do this operation?') "
发布评论

评论列表(0)

  1. 暂无评论