简体   繁体   English

如何调用确认弹出框?

[英]How do I call a confirm popup box?

I want to show an confirm popup (yes, no) if an asp.net button was clicked.如果单击 asp.net 按钮,我想显示确认弹出窗口(是,否)。

If I click yes or no it should execute some c# code.如果我单击是或否,它应该执行一些 c# 代码。

Lets say if I click yes it will insert a record and if I click no it should update the record比方说,如果我单击“是”,它将插入一条记录,如果我单击“否”,它将更新记录

Can someone give me an example on how can I achieve this?有人可以举个例子说明我如何实现这一目标吗? I'm really having a hard time since I'm new in javascript and c#.我真的很难过,因为我是 javascript 和 c# 的新人。

If you'd like to allow the user to cancel a button they just clicked on, you can use the OnClientClick attribute: 如果要允许用户取消他们刚刚单击的按钮,则可以使用OnClientClick属性:

<asp:Button runat="server" OnClientClick="return confirm('Are you sure?')" 
    OnClick="CSharpMethod" />

Where CSharpMethod is a method in your code behind that executes the C# lines you want run (when the user accepts the confirmation). CSharpMethod是您代码后面的方法,该方法执行要运行的C#行(当用户接受确认时)。

The signature of this method should look like this: 此方法的签名应如下所示:

protected void CSharpMethod(object sender, EventArgs E) {
    Response.Write("Hello From Server");
}

EDIT 编辑

If you want the true of false result of the confirmation to be sent to the server, I would recommend you first create a hidden input: 如果您希望将确认的错误结果发送给服务器,则建议您首先创建一个隐藏的输入:

<input type="hidden" runat="server" id="hdnConfirm" />

Then in your .net button 然后在您的.net按钮中

<asp:Button runat="server" OnClientClick="confirmWithUser()" 
    OnClick="CSharpMethod" />

Then create the JavaScript function to go with the OnClientClick: 然后创建与OnClientClick一起使用的JavaScript函数:

function confirmWithUser(){
    document.getElementById("hdnConfirm").value = confirm('Are you sure?');
}

In your C# method, you'll be able to get the response from the user like this: 在您的C#方法中,您可以像这样从用户那里获得响应:

protected void CSharpMethod(object sender, EventArgs E) {
    bool whatDidUserSay = Boolean.Parse(hdnConfirm.Value);

    //now do one thing or the other based on this value
}

You can not get that which button is clicked Yes or No using the button's ClientClick event through JavaScript only. 您无法仅通过JavaScript使用按钮的ClientClick事件来获得单击哪个按钮的“是或“ 否” You can control the submission of the form by returning true or false. 您可以通过返回true或false来控制表单的提交。

 btn.OnClientClick = "return confirm('Ready to submit.')";

Javascript confirm without code in code behind JavaScript确认没有代码在代码后面

That functionality you are expecting to perform that need some client side JavaScript to server side code interaction. 您期望执行的功能需要一些客户端JavaScript与服务器端代码的交互。 this may be possible check json.org . 这可能是检查json.org there are some libraries that perform such type of functionality. 有一些执行此类功能的库。

As you asked that if click on Yes then it will do some thing and if No then Update. 当你问,如果点击Yes那么它会做一些事情,如果No再更新。 you can do this using the Ajax Controls . 您可以使用Ajax Controls执行此操作。 if you are allowed to use them in your solution. 如果允许您在解决方案中使用它们。

If you are not allowed to use them then create a hidden field , and set the value of that field based on the result of the confirmation. 如果不允许使用它们,则创建一个hidden field ,然后根据确认结果设置该字段的值。

.aspx 的.aspx

<input type="hidden" id="confirmResult" name="confirmResult" value="" />
<asp:Button  OnClientClick="ConfirmDialog();" 
    OnClick="OnServerClick" runat="server" />

<script>
   function ConfirmDialog() {
      var result = Confirm("Confirm Insert?");
      // set a flag to be submitted - "true"/"false"
      // or whatever suits
      document.getElementById("confirmResult").value = result; 
   }

c# C#

protected void OnServerClick(object sender, EventArgs e) {
    bool dialogResult = Convert.ToBoolean(confirmResult.Value);
if(dialogResult)
{
//Insert
}
else
{
//Update
}
}

Check this stackoverflow thread for more details: 检查此stackoverflow线程以获取更多详细信息:
ASP.net - Button - Javascript Confirm dialog - execute SOME server-side code? ASP.net-按钮-Javascript确认对话框-执行某些服务器端代码?

Check this link - AJAX Enabled MessageBox that i have used perform such functionality using the UserControl and Ajax, that get the confirmation result at the Server side Click event. 检查此链接-我使用过的启用AJAX的MessageBox使用UserControl和Ajax执行此类功能,这些功能在服务器端Click事件中获得确认结果。

Have look on this, It is using ClientClient event of button etc but on this client side event it have used ModalPopupExtender ajax control. 看一下,它正在使用按钮等ClientClient事件,但是在此客户端事件上,它已经使用了ModalPopupExtender ajax控件。
Confirm Message Box 确认消息框

OnClick="btnSubmit_Click" OnClick="btnSubmit_Click"

onclientclick= "return confirm('Are You Sure To Submit Record?.')"; onclientclick= "return confirm('您确定要提交记录吗?')";

Or You can use或者你可以使用

onclientclick="return(window.confirm('Are You Sure To Submit Record?'))"; onclientclick="return(window.confirm('您确定要提交记录吗?'))";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM