Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

custom control derived from checkboxlist

I am trying  to make a custom control derived from the checkBoxList that will be tree control, with a checkbox and text at each leaf of the tree. I have all the javascript/css worked out, I just can't figure out how to inject the js/css between each node in the checkBoxList. If someone could point me in the right direction I would be greatful.

This is my first attempt at custom controls, but I have managed to create a control, inheriting from checkBoxList, and I can inject js/css before and after the control by overriding render, but I can't figure out how to inject between the nodes. Here is what I have so far:

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class sksCheckBoxList
    Inherits CheckBoxList

    'This works, outputting "TEST" before and after the list
    Protected Overrides Sub render(ByVal writer As HtmlTextWriter)
        writer.Write("<br>[TEST]")
        MyBase.Render(writer)
        writer.Write("<br>[/TEST]")
    End Sub

    'This does nothing
    Public Overrides Sub RenderBeginTag(ByVal writer As HtmlTextWriter)
        writer.WriteBeginTag("[RBT]")
        MyBase.RenderBeginTag(writer)
    End Sub


    'This does nothing
    Public Overrides Sub RenderEndTag(ByVal writer As HtmlTextWriter)
        writer.Write("[RET]")
        MyBase.RenderEndTag(writer)
    End Sub

End Class


Thanks for any help you can provide.

Roger
Wednesday, October 06, 2004

AFAIK there is no documented way to do this.

But if you're prepared to take the risk of possible incompatibility with future versions of the framework, I think it's possible. 

You need to use a tool such as Reflector to examine how the Render method is implemented.  In your case, you'll see:
- CheckBoxList implements IRepeatInfoUser.RenderItem
- CheckBoxList's Render method instantiates a RepeatItem object which calls CheckListBox's  IRepeatInfoUser.RenderItem method repeatedly for each checkbox to be rendered.
- CheckBoxList's implementation of IRepeatInfoUser.RenderItem renders a checkbox with the appropriate properties.

You could implement IRepeatInfoUser in your derived class and duplicate the CheckBoxList implementation, with the addition of code of your own to render the script.  The implementation uses a private "controlToRepeat" member which you can't access, so you'd need your own private member but this shouldn't be a problem (create one in your constructor: it should be a CheckList control).

Before doing this you should consider whether it is possible to:
- avoid the need to render script for each item.  Surely you can render all the script together at the end?
- possibly derive your own class from ListControl, and base your implementation on CheckBoxList, using Reflector to examine its implementation.  This removes the dependency on the CheckBoxList implementation.

Joe
Wednesday, October 06, 2004

*  Recent Topics

*  Fog Creek Home