2

I have a custom control, that is entirely dependent on jquery plugins. Everything is working fine as it is supposed to, but the problem arises when the control is set Visible = false or any of its parents is not Visible. Ideally, when an asp.net control is set Visible false, the engine doesnt render it, so my control is not being rendered on the page load. And when the control doesnt render, it does not load javascript resources, and hence jquery plugins dont bind when the control shows up on postback. This is a very basic problem, and as the developers, which are supposed to use it, they are just going to drag and drop the control on their pages, assuming it is working perfectly. I think there must be a work around for it. Pasted below is how I am loading resources.

ClientScriptManager cs = this.Page.ClientScript;

#region Loading JavaScript File(s)
string MenuPlugin = "MainMenuControl.Scripts.javascript_main.js";
cs.RegisterClientScriptResource(typeof(MainMenuControl.MenuControl), MenuPlugin);
string MenuInitializer = "MainMenuControl.Scripts.BuildMenu.js";
cs.RegisterClientScriptResource(typeof(MainMenuControl.MenuControl), MenuInitializer);
#endregion

#region Loading CSS File(s)
string cssUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "MainMenuControl.Styles.MenuStyle.css");
HtmlLink cssLink = new HtmlLink();
cssLink.Href = cssUrl;
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("type", "text/css");
this.Page.Header.Controls.Add(cssLink);
#endregion

#region Loading Image(s)
Page.ClientScript.GetWebResourceUrl(typeof(MainMenuControl.MenuControl), "MainMenuControl.Images.down.gif");
string script = string.Format(@"initializeMenu('{0}');", Page.ClientScript.GetWebResourceUrl(typeof(MainMenuControl.MenuControl), "MainMenuControl.Images.down.gif"));
Page.ClientScript.RegisterStartupScript(Page.GetType(), "InitImage", script, true);
#endregion

In the third last line of the code, I am calling a function with an image url that is loaded correctly. But for some reason, the javascript file(s) do(es)nt load and neither any browser show them in the loaded resources list.

Is there any solution to the problem, or is there anything that I am doing wrong? I am really helpless about this problem now and would appreciate any help.

Taha Rehman Siddiqui
  • 2,441
  • 5
  • 32
  • 58

1 Answers1

1

I had a very similar problem earlier and came across your question.

I was loading javascript for my custom control by overriding the OnPreRender method.

    protected override void OnPreRender(EventArgs e) {
        base.OnPreRender(e);
        Page.ClientScript.RegisterClientScriptInclude("MyJavascript", "MyURL");
    }

This had worked when using the control in a normal page environment, but in this case I was using it inside an update panel and initially hiding it. Ajax post backs from the update panel would then update the Visible property to true. At this point of course ASP.NET has no opportunity to automatically inject the client script into the <head> of the page.

The solution

Instead of overriding OnPreRender you can override OnInit (which should always fire even when the control is hidden) at this point you can add an event handler to any of the Page events. This makes you free to add an OnPreRender method which will always execute as part of the Page lifecycle.

    protected override void OnInit(EventArgs e) {
        base.OnInit(e);

        Page.PreRender += new EventHandler(Page_PreRender);
    }

    protected void Page_PreRender(object sender, EventArgs e) {
        Page.ClientScript.RegisterClientScriptInclude("MyJavascript", "MyURL");
    }
Red Taz
  • 4,159
  • 4
  • 38
  • 60