C# asp.net 运行时的确认对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1234953/
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
Confirmation dialog at runtime in asp.net
提问by Geri Langlois
I have a simple content management system that stores pages by Pagename and Version. After clicking on Save, my code (server side) checks for the existence of Pagename/Version.
我有一个简单的内容管理系统,可以按页面名称和版本存储页面。单击“保存”后,我的代码(服务器端)会检查页面名称/版本是否存在。
If it exists I would like to display a confirmation dialog box, which asks the user to confirm whether or not the current Pagename/Version should be replaced.
如果存在,我想显示一个确认对话框,要求用户确认是否应替换当前的页面名称/版本。
What is the easiest way to accomplish this? Thanks.
实现这一目标的最简单方法是什么?谢谢。
采纳答案by Geri Langlois
I appreciate both previous answers and they were helpful but not exactly what I was looking for. After considering the responses and doing more research I'm posting my solution so that maybe it will help someone else.
我很欣赏之前的两个答案,它们很有帮助,但并不完全是我想要的。在考虑了响应并进行了更多研究之后,我发布了我的解决方案,以便它可以帮助其他人。
Button code:
按钮代码:
<asp:Button ID="btnSave" OnClick="btnSaveClick" runat="server" Text="Save" OnClientClick="return CheckForVersion()" />
Javascript:
Javascript:
<script language="javascript">
function CheckForVersion() {
PageMethods.CheckForVersion(aspnetForm.ctl00$ContentPlaceHolder1$ddlPageName2.value, aspnetForm.ctl00$ContentPlaceHolder1$txtContentName2.value, OnSucceeded, OnFailed);
return false;
}
function OnSucceeded(results) {
if(results) {
//version exists so prompt user
if(confirm("Version already exists. Do you want to overwrite?")) {
__doPostBack('ctl00$ContentPlaceHolder1$btnSave','');
}
}
else
{
//version does not exist so save it without prompting user
__doPostBack('ctl00$ContentPlaceHolder1$btnSave','');
}
}
function OnFailed(error) {
// handle pagemethod error
alert(error.get_message());
}
</script>
C# using Subsonic 2.1:
使用 Subsonic 2.1 的 C#:
[WebMethod]
public static bool CheckForVersion(string pageName, string versionName)
{
PageContentCollection pages = new PageContentCollection().Where("pageName", pageName).Where("versionName", versionName).Load();
if (pages.Count > 0)
return true;
else
return false;
}
回答by Carlo
<asp:Button OnClientClick="return confirm('Are you sure you want to go?');"
Text="Confirm" runat="server" onclick="Unnamed1_Click" />
If they click OK, the server onclick event will happen, if they click cancel, it will be like they didn't even press the button, of course, you can always add functionallity to the cancel part.
如果他们点击OK,就会发生服务器onclick事件,如果他们点击取消,就像他们甚至没有按下按钮一样,当然,您可以随时为取消部分添加功能。
Maybe something like:
也许是这样的:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function CompareConfirm()
{
var str1 = "abc";
var str2 = "def";
if (str1 === str2) {
// your logic here
return false;
} else {
// your logic here
return confirm("Confirm?");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button OnClientClick="return CompareConfirm();"
Text="Confirm" runat="server" onclick="Unnamed1_Click" />
</div>
</form>
</body>
</html>
回答by svinto
Put the check before rendering the page to the client. Then attach a handler (on the client side, eg. javascript) to the save-button or form that displays the confirmation box (but only if the saving results in a replacement).
在将页面呈现给客户端之前进行检查。然后将处理程序(在客户端,例如 javascript)附加到显示确认框的保存按钮或表单(但仅当保存导致替换时)。
回答by Chris Roberts
An alternative, simpler approach which doesn't require AJAX would be to allow the post-back as normal, then in the code-behind, do your checks.
另一种不需要 AJAX 的更简单的方法是允许正常回发,然后在代码隐藏中进行检查。
If the user confirmation is required, just return the user back to the same page but make an extra panel visible and hide the original 'Save' button.
如果需要用户确认,只需将用户返回到同一页面,但使额外的面板可见并隐藏原始的“保存”按钮。
In this extra panel, display your message with another OK / Cancel button. When the user clicks this OK button, perform the save!
在这个额外的面板中,使用另一个确定/取消按钮显示您的消息。当用户单击此 OK 按钮时,执行保存!
回答by Alireza815
add a hidden field to your page for example Hiddenfield1
向您的页面添加一个隐藏字段,例如 Hiddenfield1
then add this function
然后添加这个功能
public bool Confirm(string MSG)
{
string tmp = "";
tmp = "<script language='javascript'>";
tmp += "document.getElementById('HiddenField1').value=0; if(confirm('" + MSG + "')) document.getElementById('HiddenField1').value=1;";
tmp += "</script>";
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "ConfirmBox", tmp);
if(HiddenField1.Value.Trim()=="0") return false;
return true;
}