I need to output some JavaScript in a WebControl based on some processing and some properties that the consumer can set, doing it on the load of the page will be to early.
When is the latest I can call RegisterClientScriptBlock and still have it output on the page?
Old question, but I recently dealt with this, so to give Tim's answer a bit more clarification and clear up the bit of misinformation:
If you use RegisterClientScriptBlock like you're asking, then your scripts will be output during ClientScriptManager.RenderClientScriptBlocks, which is called during Page.BeginFormRender -- but it's actually called by the Form, not by the Page itself.
Here's the sequence:
From 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();
}
}
Notice the calls to page.BeginFormRender and page.EndFormRender. Between them, the form calls it's base.RenderChildren which will eventually call the Render method on your custom User Control. So to be true to your original question, you cannot interact with the ClientScriptBlocks portion of scripts at any time during any child control's Render sequence since they've already been output to the Response stream. You can add scripts to this block during the Render sequence if you're in the Page's Render method before you call base.Render, as Tim mentions, but this does not work in a child control of any kind.
If the Render sequence is all you have to work with (this is the situation I am in) then you can use ClientScript.RegisterStartupScript during your control's Render, as the RenderClientStartupScripts is called during page.EndFormRender, which happens after your controls are told to render, as you can see in the code above.
OnPreRender
or, if you override Render.... before calling "base.Render"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With