Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirmation dialog at runtime in asp.net

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.

like image 954
Geri Langlois Avatar asked Aug 05 '09 18:08

Geri Langlois


3 Answers

<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>
like image 137
Carlo Avatar answered Oct 15 '22 00:10

Carlo


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!

like image 43
Chris Roberts Avatar answered Oct 15 '22 00:10

Chris Roberts


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;            
    }
like image 7
Geri Langlois Avatar answered Oct 15 '22 00:10

Geri Langlois