Attention: We are retiring the ASP.NET Community Blogs. Learn more >

A better design time experience

I'm sure that Andy Smith has probably trademarked the term "A better design time experience" but, anyways here goes...

Last night I had to build my first production quality server control.  My control had to be able to bind to a DataTable and render links of data with an associated image based on the value of the data - i.e.: < 50 : Red Traffic Light; < 75 - Yellow Traffic Light; >= 75 - Green Traffic Light.

I decided that my control would derive from Web.UI.WebControls.Table - which is great because you get all of the correct property grid information to allow users to stylize the control at design time.

I also decided that, rather than attempting to display live data in the designer that I'd use a custom designer and display a static representation of my control.  Finally, I needed the table in my designer class to respond to style changes as the user made them; after some assistance from Andy, I found this way of copying the Styles directly from the component through into my designer class:

 

    Public Overrides Function GetDesignTimeHtml() As String
        Dim sw As New StringWriter
        Dim writer As New HtmlTextWriter(sw)
        Dim t As New Table
        t.ControlStyle.CopyFrom(CType(Component, Table).ControlStyle)

        t.Rows.Add(BuildHeaderRow())
        t.Rows.Add(BuildRow("KPI 1", "###", "###"))
        t.Rows.Add(BuildRow("KPI 2", "###", "###"))
        t.Rows.Add(BuildRow("KPI 3", "###", "###"))
        t.RenderControl(writer)

        Return sw.ToString()
    End Function

No Comments