Jon Roar Odden
Jon Roar Odden

Reputation: 23

Stop javascript from executing on page load and execute from ASP.NET codebehind

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?

Upvotes: 2

Views: 2215

Answers (2)

Kieran Quinn
Kieran Quinn

Reputation: 1115

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);
}

Upvotes: 0

Moshe Karmel
Moshe Karmel

Reputation: 459

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

Upvotes: 1

Related Questions