开发者

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

开发者 https://www.devze.com 2022-12-22 02:04 出处:网络
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.

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:

  • Check a ControlManager control exists on the page if some other JQueryControls do
  • Add each control to a collection within the ControlManager, assuming the an开发者_运维技巧swer to the first point does not return the JQueryControlManager instance

OR

Does anyone have a suggestion for a better solution?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号