最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Confirmation dialog at runtime in asp.net - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Aug 5, 2009 at 18:32 Geri LangloisGeri Langlois 6861 gold badge9 silver badges21 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 16
<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.

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:

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

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

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

add a hidden field to your page for example 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;
}
发布评论

评论列表(0)

  1. 暂无评论