2

First; I'm trying to do two things in my ASP.NET / JavaScript code.

1) open a jQuery dialog from ASP.NET codebehind. (Works only when the javascript also executes on startup)

2) stop the same javascript/jQuery dialog as mention in 1) from showing on every pageload.

So the code below works great, but executes on page load which I try to stop. In addition this format (without function name) won't let me call the function from codebehind if I understand this How can I prevent this jQuery function from being executed on every page load? correctly:

$(function() {
        $( "#dialogConfirm" ).dialog({
            resizable: false,
            height:180,
            width: 500,
            modal: true,
            buttons: {
                "Delete": function() {
                    $( this ).dialog( "close" );
                },
                "Cancel": function() {
                    $( this ).dialog( "close" );
                }
            }
        });
    });

and this doesn't work (just shows the div on the page and not as a jQuery dialog):

$(function showConfirmDialog() {
        $( "#dialogConfirm" ).dialog({
            resizable: false,
            height:180,
            width: 500,
            modal: true,
            buttons: {
                "Delete": function() {
                    $( this ).dialog( "close" );
                },
                "Cancel": function() {
                    $( this ).dialog( "close" );
                }
            }
        });
    });

The only difference is the lack of function naming in the first. So, can anyone point me in the right direction as to how to just make a script that I can call from codebehind that won't execute on page load?

1
  • you could put it in a function and then from your code behind add some script to the page that calls the function Commented Dec 4, 2015 at 16:19

2 Answers 2

1

Add a function with a name like "showDialog" in your aspx page.

function showDialog(){
    $( "#dialogConfirm" ).dialog({
        resizable: false,
        height:180,
        width: 500,
        modal: true,
        buttons: {
            "Delete": function() {
                $( this ).dialog( "close" );
            },
            "Cancel": function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

Then call it from your codebehind using the function name:

ScriptManager.RegisterStartupScript(this, typeof(Page), "", "showDialog", true);
Sign up to request clarification or add additional context in comments.

Comments

0

Change your js function to this:

function ShowDialog() {
    $( "#dialogConfirm" ).dialog({
        resizable: false,
        height:180,
        width: 500,
        modal: true,
        buttons: {
            "Delete": function() {
                $( this ).dialog( "close" );
            },
            "Cancel": function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

Then in your .cs file, put this in the Page_Load

if (IsCallback)
{
    ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:ShowDialog();", true);
}

3 Comments

Thank you both for the interest. I get it to partly work with Kierans ClientScript.RegisterStartupScript approach. But the div still shows on the page before I call the javascript to display it as a dialog (then it disappears from the page, and displays as a dialog instead. This is the HTML div in my aspx page: <div id="dialogConfirm" title="Delete?"> <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p> </div>
It's also relevant to mention that after running the js from codebehind once, all works great after that. The div doesn't re-appear on page after that. Please also see screenshots here: imgur.com/gallery/tXugH
Fixed the last remaining "issue" I had by changing <div id="dialogConfirm" title="Delete?" > to <div id="dialogConfirm" title="Delete?" style="display:none">

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.