UpdatePanel and CssClass

I never really understood why the UpdatePanel was derived from Control instead of WebControl. It obviously represents a more or less visible web element but lacks the possibility to add class or style tags from codebehind. Maybe it is due to the updateable nature of the control, I don’t know. The only thing I now right now is that I want a CssClass property on my UpdatePanel. Thankfully thats an easy task, just create a new Control that derives from the UpdatePanel, add a „CssClass“ Property and add the contents to the attribute collection in the render method. This is how the class would look like (note that I use ASP.NET 3.X notation):


public class CustomUpdatePanel : System.Web.UI.UpdatePanel
{
    public string CssClass
    {
        get;
        set;
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (CssClass != string.Empty)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
        }

        base.Render(writer);
    }
}

I like it 😉 and it also comes really handy for UpdateProgress.