Skip to content Skip to sidebar Skip to footer

Define Javascript In Ascx Control Or On Parent Page?

I am having some issues with javascripts that don't work. My asp.net 3.5 webforms app has a page that contains several user controls (ascx) which contain javascript. Not all of the

Solution 1:

I would use RegisterStartupScript in the controls and let ASP.NET work for you. Also, this will prevent lots of problems in the event you have multiple instance of the same control on a page:

    String csname1 = "PopupScript";
    Type cstype = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {    
        cs.RegisterStartupScript(cstype, csname1, "alert('Hello World!')", true);
    }

Post a Comment for "Define Javascript In Ascx Control Or On Parent Page?"