RegisterClientScriptBlock in a widget

Topics: ASP.NET 2.0, Controls
Aug 19, 2008 at 8:36 PM
Edited Aug 20, 2008 at 12:46 AM
I'm looking to create my own widget to add to the BlogEngine.net site. The only problem is that when I try to register a client script block the script isn't registered. I tested my widget (aka .ascx) on a test aspx page with my class inheriting from UserControl not WidgetBase and all is good. As soon as I inherit from WidgetBase the scripts are not registered. There aren't any exceptions throw, the script just isn't showing up in the rendered page. Here's what I have...

public partial class widgets_Mywidg_widget : WidgetBase
{
    protected override void OnInit(EventArgs e)
    {
       if (!Page.ClientScript.IsClientScriptBlockRegistered(typeof(WidgetBase), "WLP_API_Callback"))
          Page.ClientScript.RegisterClientScriptBlock(typeof(WidgetBase), "WLP_API_Callback", CallbackScript, false);

       if (!Page.ClientScript.IsClientScriptBlockRegistered(typeof(WidgetBase), "WLP_API_PrimaryQuery"))
          Page.ClientScript.RegisterClientScriptBlock(typeof(WidgetBase), "WLP_API_PrimaryQuery", QueryScript, false);

        base.OnInit(e);
    }
}...

(CallbackScript and QueryScript are strings that hold the javascript if that wasn't obvious)

I tried the same in the LoadWidget method with no luck. Also tried base.Page.ClientScript.RegisterClientScriptBlock... just for the sake of trying to get it to work. The only thing I could come up with is there is something going on in the RewriteFormHtmlTextWriter class. Any idea?

Aug 20, 2008 at 7:17 PM
Edited Aug 20, 2008 at 8:19 PM
I used Page.ClientScript.RegisterStartupScript and it works! Don't forget to change your if statement to Page.ClientScript.IsStartupScriptRegistered...