简体   繁体   English

asp.net中运行时的确认对话框

[英]Confirmation dialog at runtime in asp.net

I have a simple content management system that stores pages by Pagename and Version. 我有一个简单的内容管理系统,通过Pagename和Version存储页面。 After clicking on Save, my code (server side) checks for the existence of Pagename/Version. 单击Save后,我的代码(服务器端)检查是否存在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. 如果存在,我想显示一个确认对话框,要求用户确认是否应该替换当前的Pagename / Version。

What is the easiest way to accomplish this? 实现这一目标的最简单方法是什么? Thanks. 谢谢。

<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>

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: C#使用Subsonic 2.1:

[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;            
    }

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! 当用户单击此“确定”按钮时,执行保存!

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)附加到显示确认框的保存按钮或表单(但仅当保存导致替换时)。

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;
}

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

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