title: How call GetWebResourceUrl() when Page is null ?
body: <p>I built custom ASP.NET control and it's working fine when I add it manually (drag and drop) or by code to controls in markup.<br>
  The custom control, MsgBox, had resources like JavaScript, CSS and images embedded in and the problem appeared when I tried to Render the control in class to return its HTML code, the Page instance is null and the "GetWebResourceUrl" needs it:  </p>
  
  <pre><code>Page.ClientScript.GetWebResourceUrl(.....)  
  </code></pre>
  
  <p>is there any way to get the resourceurl ? Here is my render code:  </p>
  
  <pre><code>protected override void RenderContents(HtmlTextWriter writer)
  {
      using (PlaceHolder plh = new PlaceHolder())
      {
          if (Page != null)
          {
              if (DesignMode || Page.Header == null)
                  RegisterCSSInclude(plh);
          }  
          HtmlGenericControl container = new HtmlGenericControl("div");
          container.EnableViewState = false;
          container.InnerHtml = "Control html code";  
          plh.Controls.Add(container);
          plh.RenderControl(writer);
      }
  }
  </code></pre>
  
  <p>RegisterCSSInclude is method to register my css files:  </p>
  
  <pre><code>private void RegisterCSSInclude(Control target)
  {
      // CSS                   
      bool linkIncluded = false;
      foreach (Control c in target.Controls)
      {
          if (c.ID == "MsgBxStyle")
          {
              linkIncluded = true;
          }
      }
      if (!linkIncluded)
      {
          HtmlGenericControl globalCsslink = new HtmlGenericControl("link");
          globalCsslink.ID = "MsgBxGStyle";
          globalCsslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(MessageBoxCtrl), "MessageBox.MsgBxStyles.WeDevMsgBox.css"));
          globalCsslink.Attributes.Add("type", "text/css");
          globalCsslink.Attributes.Add("rel", "stylesheet");
          globalCsslink.EnableViewState = false;
          target.Controls.Add(globalCsslink);  
          HtmlGenericControl csslink = new HtmlGenericControl("link");
          csslink.ID = "MsgBxStyle";
          csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(MessageBoxCtrl), "MessageBox.MsgBxStyles." + Style.ToString().ToLower() + ".css"));
          csslink.Attributes.Add("type", "text/css");
          csslink.Attributes.Add("rel", "stylesheet");
          csslink.EnableViewState = false;
          target.Controls.Add(csslink);
      }
  }  
  </code></pre>
  
  <p><strong>Update:</strong><br>
  PS: I'm tring to use control in generic handler (ashx) where I call ShowMsgBox method which is a method in a class and not in a page or user control.<br>
  ShowMsgBox method should create an instance of MsgBox control and render it then return the html code to ashx class :  </p>
  
  <blockquote>
    <p>var htmlCode = MyClass.ShowMsgBox("myMsg");<br>
    context.Response.write(htmlCode);</p>
  </blockquote>