Ed James
Ed James

Reputation: 10607

Throw an error if a dependent control is not present on the page

Ok, I'm making a couple of JQuery versions of the AJAXToolkit controls (we were having issues with the toolkit), and want to have a common version of the scripts they require on the page.

To do this, I was intending to have a JQueryControlManager control, and use it to insert scripts dynamically, preferably only scripts that are needed by controls on the page.

Does anyone know of an efficient way to:

OR

Does anyone have a suggestion for a better solution?

Upvotes: 1

Views: 109

Answers (1)

EMP
EMP

Reputation: 61971

For scripts specifically, ASP.NET already has a nice ScriptManager class that supports what you're trying to do. Call

Page.ClientScript.RegisterClientScriptInclude(key, url);

from your control with a unique key for each script and the framework will take care of avoiding duplicates. There are other similar methods on ClientScriptManager that may be useful.

If you still need to create an instance of your ControlManager you could either just enumerate over the Page.Controls collection looking for a control of that type, ie.

public ControlManager GetControlManager()
{
    foreach (Control control in Page.Controls)
    {
        if (control is ControlManager)
            return (ControlManager)control;
    }

    ControlManager manager = new ControlManager();
    Page.Controls.Add(manager);

    return manager;
}

or you could try storing the instance of ControlManager in the Page.Items dictionary.

Warning: I haven't tested this code, so treat it as pseudo-code.

Upvotes: 1

Related Questions