C# 如何从后面的代码中的确认框获得响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11643515/
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 get the response from Confirm box in the code behind
提问by Soumya
I am new to asp.net/C# .I am trying to create a web application.
我是 asp.net/C# 的新手。我正在尝试创建一个 Web 应用程序。
Below is my requirement.
下面是我的要求。
I am trying to save a record on button click. Before saving the record,I will be checking if that record exist in the database or not(in the code behind).If it exist,then I need to show an alert to the user as "Record already exist.Do you want to proceed?"When the user press 'Yes',I need to continue my save for the record in the code ,else I just need to exit the save process.
我正在尝试在单击按钮时保存记录。在保存记录之前,我将检查该记录是否存在于数据库中(在后面的代码中)。如果存在,那么我需要向用户显示一个警告“记录已经存在。你想继续吗?” ?”当用户按‘是’时,我需要继续保存代码中的记录,否则我只需要退出保存过程。
//......code for checking the existence of the record
if (check == true)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", " confirm('Record already exist.Do you want to proceed?');", true);
}
//
The above code shows me confirm box with 'OK' and 'Cancel' buttons. My questions are
上面的代码向我展示了带有“确定”和“取消”按钮的确认框。我的问题是
- how can I make it 'Yes' or 'No' in the confirm dialog?
- After the user press 'Yes'/'No',how can I catch the response(yes/no),and proceed with rest of my program?
- 如何在确认对话框中将其设为“是”或“否”?
- 用户按下“是”/“否”后,我如何捕捉响应(是/否),然后继续我的程序的其余部分?
I have searched for this a lot.But couldn't get a proper answer.Please help me on this.
我已经搜索了很多。但无法得到正确的答案。请帮助我。
回答by Ajeet Pratap Maurya
you can take a look at the this link http://www.codeproject.com/Articles/8173/A-Simple-ASP-NET-Server-Control-Message-Box-Confir.
你可以看看这个链接http://www.codeproject.com/Articles/8173/A-Simple-ASP-NET-Server-Control-Message-Box-Confir。
And more over you can create your own custom confirmation box.
此外,您还可以创建自己的自定义确认框。
回答by Umesh
You can use confirm box in JS like this
您可以像这样在 JS 中使用确认框
var ans = confirm ('Record already exist.Do you want to proceed?');
if(ans==true)
{
}
else
{
}
Secondly, to get the response in code behind, you can store the Yes/No value into a hidden field e.g.
其次,要在后面的代码中获得响应,您可以将 Yes/No 值存储到隐藏字段中,例如
document.getElementById('<%= hiddenField.ClientID %>').value = ans;
回答by Shant
One of the workaround can be to store the TRUE / FALSE flag in control that has the attribute runat="server", depending on the choice made by user (Yes / No). And then at the backend, you can check for the value of this control.
一种解决方法是将 TRUE/FALSE 标志存储在具有 runat="server" 属性的控件中,具体取决于用户所做的选择(是/否)。然后在后端,您可以检查此控件的值。
回答by Yograj Gupta
You can do this in many ways:
您可以通过多种方式执行此操作:
One is place a button on the page, make its display:none and when confirm is true, trigger its click with js.
一种是在页面上放置一个按钮,使其显示:无,当确认为真时,用js触发其点击。
Like in aspx
就像在aspx中一样
<asp:Button runat="server" ID="btnSaveData"
onClick="btnSaveData_Click" style="display:none;" />
in client side make a js function for calling confirmation dialog box, like
在客户端做一个调用确认对话框的js函数,比如
function ConfirmSave()
{
if(confirm('Record already exist.Do you want to proceed?')
{
jQuery("[ID$=btnSaveData]").click();
}
}
in Code Behind
在代码背后
Your code check in some event handler
您的代码检查某些事件处理程序
if (check == true)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script",
"ConfirmSave();", true);
}
bthSaveData click handler for saving data.
bthSaveData 单击处理程序用于保存数据。
protected void btnSaveData_Click(object sender, EventArgs e)
{
// Code for your saving.
}
回答by Abhijeetchindhe
Use AJAX toolkit with modalpopupextender.
将 AJAX 工具包与 modalpopupextender 结合使用。
<cc1:modalpopupextender id="ModalPopupExtender1" runat="server" cancelcontrolid="ButtonNo" okcontrolid="ButtonYes" popupcontrolid="PNL" targetcontrolid="UrSaveButton">
</cc1:modalpopupextender>
<asp:Panel ID="PNL" runat="server" Style="display: none; width: 200px; background-color: #000099; border-width: 2px; border-color: Black; border-style: solid; padding: 20px;">
<span style="color: White; font-weight: bold">Record already exist.Do you want to proceed?</span>
<br />
<br />
<div style="text-align: right;">
<asp:Button CssClass="tbl_blue" ID="ButtonYes" runat="server" CausesValidation="false" EnableTheming="false" Text="YES" />
<asp:Button CssClass="tbl_blue" ID="ButtonNo" runat="server" CausesValidation="false" EnableTheming="false" Text="NO" />
</div>
</asp:Panel>
You can Customize it as you want.
您可以根据需要自定义它。

