javascript 确认消息框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4933969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
confirm message box
提问by user175084
I have to show a yes no popup messagebox for a function>
我必须为一个函数显示一个 yes no 弹出消息框>
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:
编辑部分:
- Navigate to a page
- Add values to a textbox
- Click "Save" Button to add value to database
- Ask the user if he is sure he want to do it 'are you sure'?,, the confirm pop up
- Now the above confirm box will only happen if the ID is != 0 or else there is no need for a popup.
- if he says yes then add to database and show alert popup that values have been enterd in the DB.
- if NO then just dont add to Db and just return.
- 导航到一个页面
- 向文本框添加值
- 单击“保存”按钮将值添加到数据库
- 询问用户他是否确定要这样做“你确定吗?”,弹出确认
- 现在上面的确认框只有在 ID 为 != 0 时才会出现,否则不需要弹出窗口。
- 如果他说是,则添加到数据库并显示已在数据库中输入值的警报弹出窗口。
- 如果否,则不要添加到 Db 并返回。
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);
回答by Mike Cole
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:
是否有您点击的按钮来触发操作?如果是这样,您应该将客户端事件添加到您的 Web 控件中,如下所示:
<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.
如果用户选择是,回发照常进行。如果他们选择否,则偶数将被取消并且不会发生回发。IMO,这是处理它的方法,因为如果他们选择否,它会阻止任何额外的服务器活动。
回答by Karel
Add a linkbutton.
添加链接按钮。
In the OnClientClick add
在 OnClientClick 添加
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)
然后在按钮后面的代码(OnClick)中进行服务器端处理:(只有在单击是时才会执行)
if (ID != 0)
{
Perform function
}
回答by Rob
It appears that what you're trying to do is (in a simplified scenario):
看来您要做的是(在简化的情况下):
- Have the user navigate to Page.aspx
- Check the value of
ID(lets assume it's a querystring parameter) - If the value of
IDis non-zero, prompt the user to confirm - If they confirm do "something"
- 让用户导航到 Page.aspx
- 检查的值
ID(假设它是一个查询字符串参数) - 如果 的值
ID非零,提示用户确认 - 如果他们确认做“某事”
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 IDand do something will already have been "skipped over"
您犯的错误是试图在代码隐藏中同时处理 2、3 和 4。您(通过调用RegisterStartupScript)发出的脚本在整个页面呈现回用户之前不会执行,此时用于检查 的值和执行某些操作的第 3 步和第 4 步的代码将已被“跳过”超过”ID
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:
您需要做的是决定如何将客户端和服务器端之间的工作分开,因为您尝试做的事情是行不通的。在不知道您的页面如何工作以及 ID 值来自何处的情况下,我无法给出具体示例,但是,例如:
- Have your page check
IDto see if it hits your criteria, if it does, emit the javascript, but with some additionaljavascript that checks the response to the prompt and causes the page to re-submit but with confirmed=yes added on the querystring - Have your page check the querystring parameter "confirmed" to see if it's yes. If it is, THEN do the work
- 让您的页面检查
ID它是否符合您的标准,如果符合,则发出 javascript,但使用一些额外的javascript 来检查对提示的响应并导致页面重新提交,但在查询字符串中添加了 Confirmed=yes - 让您的页面检查查询字符串参数“已确认”以查看是否为“是”。如果是,那么做工作
回答by Hans Ke?ing
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.
你不能这样做。RegisterStartupScript 只是注册要在最终呈现页面时包含的脚本。在呈现(到 HTML)之后,html 被发送到浏览器。因此,当用户最终看到该弹出窗口时,您的代码早就完成了。
EDIT:
编辑:
See the answerby Mike C.: you need to move that confirm to just beforethe submit.
请参阅Mike C.的回答:您需要将确认移动到提交之前。
回答by Richard Marskell - Drackir
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.
看到这里的问题是,不回传,就无法得到确认框的值。JavaScript 在客户端运行,并且在发生回发之前(通过 ajax 或常规方式),它无法与您的 C# 文件“对话”。
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().
您需要做的是在 JavaScript 中添加一个确认框,如果单击 Yes,它将回发到您的 Asp.net 页面并通过 Ajax 或 (example) 运行代码form.submit()。
回答by Genady Sergeev
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:
Page.ClientScript.RegisterStartupScript 将在客户端注册该脚本。据我所知,您正在服务器上执行 if 语句。您能否具体说明确认不起作用是什么意思?是否显示警告框,但按下是/否没有效果?如果是,请在客户端上移动 if ... else 语句。无论如何,我建议您用以下代码替换 RegisterStartupScriptBlock:
ClientScript.RegisterClientScriptBlock
(this.GetType(), "confirm", "alert('do')", true);

