views:

495

answers:

5

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.

+2  A: 
<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>
Carlo
I think he want to compare at server side rather in javascript.
Muhammad Akhtar
A: 

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

svinto
The user enters the pagename/version name in a textboxes so I'm not sure how I would check before rendering. I suppose I could use a pagemethod in the javascript for the check but is this the best way?
Geri Langlois
If you're just checking two strings, you can do it in javascript, do a CompareConfirm() method in JS and call it on the asp:Button OnClientClick event, compare the two strings and have the method return the Confirm(). I'll elaborate a little more in my answer below.
Carlo
+1  A: 

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;            
    }
Geri Langlois
A: 

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!

Chris Roberts
A: 

Geri Langlois,can you please post full sample code