0

I have created a Modal Dialog Popup in jQuery/javascript in a Visual Web Part which shows every time the page loads. There is a submit button in the popup and when it is clicked, I store the username and the date at which it was acknowledged in a custom list. jQ code which works:

 <script language="javascript" type="text/javascript"  src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">

</script>
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        ExecuteOrDelayUntilScriptLoaded(openDialog, "SP.js");        
    });

    //open dialog



    // Call openDialog method on button click or on page load  

    function openDialog() {
        //alert("In funciton opendialog");
        var options = {

            //html: divModalDialogContent,  // ID of the HTML tag

            // or HTML content to be displayed in modal dialog

            width: 400,

            url: "/_layouts/ModalDialog1/ModalDialog.aspx",

            height: 125,

            title: "Acknowedgement Popup",

            dialogReturnValueCallback: dialogCallbackMethod,  // custom callback function

            allowMaximize: false,

            showClose: false

        };
        SP.UI.ModalDialog.showModalDialog(options);

    }


    // Custom callback function after the dialog is closed

    function dialogCallbackMethod(result, returnValue) {

        alert("dialogResult" + result + "nreturnValue" + returnValue);
                if(result == SP.UI.DialogResult.OK)
         {
         alert("You chose the OK button");
         //document.title = returnValue;
         }

        if(result == SP.UI.DialogResult.cancel)
         SP.UI.Notify.addNotification("You chose the Cancel button");
         }

    }

</script>


Now, I'm adding the username in the list with this code. I'm storing it in the list using an aspx page which is triggered when the button is clicked in popup:


protected void Button1_Click(object sender, EventArgs e)
    {
        Context.Response.Write(@"<script type='text/javascript'>
                                alert('Thanks for acknowledging!');
                                window.frameElement.commitPopup();
                                </script>");
        Context.Response.Flush();
        Context.Response.End(); 

        string username;
        using (SPSite site = new SPSite(SPContext.Current.Web.Url))
        {
            site.AllowUnsafeUpdates = true;
            using (SPWeb web = site.RootWeb)
            {
                web.AllowUnsafeUpdates = true;
                SPUser user = web.CurrentUser;

                username = user.LoginName.ToString();


                //adding list item
                SPList l = web.Lists["Acknowledgements"];
                SPListItem li = l.Items.Add();
                li["User"] = username;
                li["Acknowedgement Date"] = System.DateTime.Today;
                li.Update(); 

            }


        }

Now the client requirement is to show the modal dialog popup at the start of the month and not show the modal dialog popup for that month if the username already exists in the custom list. I googled and came to know about registerstartupscript by which you can write javascript in code behind and used it but it isn't working. I'm planning on comparing the username from the list and the currently logged in username and not show the popup if he has already acknowledged but it beats me how I'd achieve that, moreover, with the javascript being in the client side.

string Script = @"function openDialog() { var options={
                    width:400,
                    url: '/_layouts/ModalDialog/ModalDialog.aspx', height:125,
                    title: 'Acknowedgement Popup',
                    dialogReturnValueCallback: dialogCallbackMethod, 
                    allowMaximize: false,
                    showClose: false
                    };
                    SP.UI.ModalDialog.showModalDialog(options);
                     }";Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "openDialog", Script, true); 


Am I going the right way with all this? I'm kinda new to SharePoint so please help. This has me in a predicament. Thanks in advance for all your help.

1 Answer 1

0

In your markup, you can add a Literal as a placeholder for a JS flag variable... make sure to add it before your openDialog function:

<asp:Literal runat="server" ID="DlgFlag"></asp:Literal>

In your codebehind, check against the list, and if the username is found, output a flag to the literal:

if (isUsernameInListAlready)
{
    DlgFlag.Text = "<script type=\"text/javascript\">var dlgFlag = true;</script>";
}

In your openDialog function, check for this flag at the top, and simply exit w/o opening the dialog if the flag is present:

function openDialog() {
    if (dlgFlag) return;

    //alert("In funciton opendialog");
    var options = {
    ...
4
  • Thanks for the reply! I'll be sure to check it out. But how do i show the popup at the start of each month and would i need a custom timer job if i want to send emails monthly as well?
    – Akhoy
    Commented Dec 28, 2012 at 17:05
  • @uberz91 When you query the list with the username records, just query for records with a created date within the current month. That way, if a new month has started since the last time the user hit the site, they won't be returned in the query, and you can safely not set the flag for that user, as if they were new. Commented Dec 28, 2012 at 22:29
  • Thanks a lot! Worked like a charm but I still didn't understand why you used a literal to set the flag.
    – Akhoy
    Commented Dec 31, 2012 at 8:28
  • I use a literal as a placeholder to output text form codebehind is all; it's certainly not the only way to do it. Commented Jan 3, 2013 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.