SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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!");
share|improve this answer
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 '12 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 '12 at 7:55
Glad I could help. – Nadeem Yousuf Nov 26 '12 at 6:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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