I must run a popup warning when somebody exceeds some value. I tried a SharePoint Modal Popup using JavaScript, but I don't know how to run something from code behind with JavaScript and a SharePoint Modal window. I write a 3 ASPX elements:

<a title="Open Dialog" href="javascript:ShowWelcomeDialog();" id="ErrorPopup" runat="server">Open Dialog</a><br /> <asp:HyperLink ID="HyperLink1" NavigateUrl="javascript:ShowWelcomeDialog();" runat="server">HyperLink</asp:HyperLink><br /> <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>

How to run from Code Behind a Hyperlink, or a Link Button or how to add a JavaScript to the Button_Click Please help.

1 Answer 1


In the code behind the code may look like this:

   var url = SPContext.Current.Web.Url + "/_layouts/myPage.aspx;
   ScriptManager.RegisterStartupScript(this.Page, typeof(Page),
   "CustomScript", "OpenDialog('" + url + "', 994, 1000, 'Title of the script', null);", true);

And the Javascript code will be like this:

    function OpenDialog(strPageURL, width, height, title, args) {

            var dialogOptions = SP.UI.$create_DialogOptions();
            dialogOptions.url = strPageURL; // URL of the Page
            dialogOptions.width = width; // Width of the Dialog
            dialogOptions.height = height; // Height of the Dialog
            dialogOptions.title = title;
            dialogOptions.args = args;
            dialogOptions.allowMaximize = false;
            dialogOptions.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback); // Function to capture dialog closed event
            SP.UI.ModalDialog.showModalDialog(dialogOptions); // Open the Dialog
            return false;

      // Dialog close event capture function
        function CloseCallback(strReturnValue, target) {

            if (strReturnValue === SP.UI.DialogResult.OK) // Perform action on Ok.
                 //alert("User clicked Ok!");
            if (strReturnValue === SP.UI.DialogResult.cancel) // Perform action on Cancel.
                //alert("User clicked Cancel!");
  • It shows me, what should I write to url ?: Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /_layouts/Event.aspx
    – Grzegorz Z
    Nov 25, 2012 at 7:24
  • Thanks :) I done it by this line of code: ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "CustomScript", "ShowWelcomeDialog();", true); You are very helpful :)
    – Grzegorz Z
    Nov 25, 2012 at 7:55
  • 1
    Glad I could help. Nov 26, 2012 at 6:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.