javascript 如何在 ASP.NET 中制作一个简单的是/否弹出窗口,将结果返回给我的 c#?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5867589/
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
How to make a simple yes/no popup in ASP.NET that return the result back to my c#?
提问by radbyx
With ASP.NET, how do I prompt the user for a yes/no question and getting the result back to my .ascx?
使用 ASP.NET,如何提示用户输入是/否问题并将结果返回到我的 .ascx?
So far I can open a confirmation dialog with use of Javascript, but I can't return the value. But I don't know if this is the right approach.
到目前为止,我可以使用 Javascript 打开一个确认对话框,但我无法返回该值。但我不知道这是否是正确的方法。
采纳答案by Sergey Metlov
You can use standart JavaScript confirm()
function to show popup and do Post Back in case of Yes or No. For example:
您可以使用标准 JavaScriptconfirm()
函数来显示弹出窗口并在 Yes 或 No 的情况下执行 Post Back。例如:
if (confirm('Question')) {
__doPostBack('', 'Yes_clicked');
} else {
__doPostBack('', 'No_clicked')
}
Then on server in Page_Load()
method do:
然后在服务器上的Page_Load()
方法做:
if (IsPostBack)
{
var result = Request.Params["__EVENTARGUMENT"];
}
You can also do it async by specifying the first parameter of __doPostBack()
function as ID of any update panel.
您还可以通过将__doPostBack()
函数的第一个参数指定为任何更新面板的 ID 来异步执行。
回答by Farzin Zaker
This is not a good practice to do this. you can get your confirm using javascript and postback or callback result to server.
这不是一个好的做法。您可以使用 javascript 和回发或回调结果获得确认到服务器。
but if you want to do this, this will help you :
但如果你想这样做,这将帮助你:
A Simple ASP.NET Server Control: Message Box & Confirmation Box
回答by Vikky
add this in head of source
将此添加到源头
function confirm_Edit() { if (confirm("Are you sure want to Edit?")==true) return true; 否则返回假;}call it like this
像这样称呼它
回答by jhovgaard
If you insist on using webforms, another solution could be the AJAX Control kit. Simply create a ModalPopup and have you confirm buttons inside that.
如果您坚持使用 webforms,另一种解决方案可能是 AJAX 控制工具包。只需创建一个 ModalPopup 并确认其中的按钮。
Read more here: http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ModalPopup/ModalPopup.aspx
在此处阅读更多信息:http: //www.asp.net/ajax/ajaxcontroltoolkit/Samples/ModalPopup/ModalPopup.aspx
回答by Janspeed
I use this. As far as I know it prevents the rest of the button event from executing.
我用这个。据我所知,它会阻止按钮事件的其余部分执行。
btnMyButton.Attributes.Add("onClick", "return confirm('Are you really sure?')");
回答by elfekz
Another option is to show yes/no:
另一种选择是显示是/否:
<script>
function AlertFunction() {
if (confirm('Are you sure you want to save this thing into the database?')) {
$('#ConfirmMessageResponse').val('Yes');
} else {
$('#ConfirmMessageResponse').val('No');
}
}
</script>
to handle it from .net side:
从 .net 端处理它:
string confirmValue = ConfirmMessageResponse.Value;
if (confirmValue == "Yes")
{...}
回答by shrutyzet
You need to use ajax, or to make a postback to the server. Your c# code is server side and the javascript is client side. If you use the ajax extensions for asp .net you can use javascript page methods:
您需要使用ajax,或者回发到服务器。您的 c# 代码是服务器端,而 javascript 是客户端。如果您使用 asp .net 的 ajax 扩展,您可以使用javascript 页面方法:
PageMethods.YourMethod(confirm('your text'), OnSuccess, OnFailure);