While this question may be old, I recently encountered the same issue and wanted to provide some additional insight regarding Tim's response.
If you opt to utilize RegisterClientScriptBlock as you inquired, your scripts will be generated during ClientScriptManager.RenderClientScriptBlocks, which is triggered by Page.BeginFormRender -- however, it is the Form that actually calls this method, not the Page itself.
Here is the series of events:
- Page.ProcessRequestMain (upon reaching the render section) invokes
- (Page's base class) Control.RenderControl which triggers
- Control.RenderChildren looping through all child controls eventually leading to
- HtmlForm.RenderControl which subsequently leads to
- HtmlForm.RenderChildren being the crucial step we are focusing on
Referring to Reflector:
protected internal override void RenderChildren(HtmlTextWriter writer)
{
Page page = this.Page;
if (page != null)
{
page.OnFormRender();
page.BeginFormRender(writer, this.UniqueID);
}
base.RenderChildren(writer);
if (page != null)
{
page.EndFormRender(writer, this.UniqueID);
page.OnFormPostRender();
}
}
Note the invocations to page.BeginFormRender and page.EndFormRender. In between them, the form calls its base.RenderChildren which ultimately invokes the Render method on your custom User Control. Therefore, sticking to the original question, you are unable to interact with the ClientScriptBlocks segment of scripts at any point within the Render sequence of any child control since they have already been outputted to the Response stream. However, you do have the ability to include scripts in this block during the Render sequence if you are within the Page's Render method before calling base.Render, as mentioned by Tim. Alas, this approach does not apply to any type of child control.
In case you solely rely on the Render sequence (which mirrors my current predicament), you can resort to using ClientScript.RegisterStartupScript within your control's Render. This is viable as RenderClientStartupScripts is triggered during page.EndFormRender, following the rendering of your controls, as evidenced in the aforementioned code snippet.