subclassing web controls

Discussion in 'ASP .Net Web Controls' started by Timo, Apr 18, 2004.

  1. Timo

    Timo Guest

    I am trying to use as a guide an example in the VBNET Developer's Guide
    by McManus and Kinsman (ch 10,) on Subclassing Existing Server Controls.
    Rather than create a User Control (ascx) or a Composite Control, I want
    to make a simple extension to the CheckBoxList control class using
    inheritance. The inherited version of the control returns the sum of the
    values checked, and allows me to assign an integer to the control's Bits
    property and then the control will figure out which items to check. [The
    values of the list items are actually bits (2^0, 2^1, 2^2, 2^3, etc).
    See code below.]

    I created the class in my project (which had some forms and other
    classes in it) and compiled the specific class file into a DLL as
    follows:

    vbc CustomControls.vb /t:library /out:CustomControls.dll
    /r:System.dll /r:System.Web.dll

    I copied the DLL into the \bin directory and in my project I added a
    reference to the compiled DLL. Then I added a Register Directive to my
    aspx form:

    <%@ Register TagPrefix="timo" namespace="MyCustomControls"
    Assembly="CustomControls" %>

    And then I added an instance of the inherited control to my form's HTML:

    <timo:BitCheckList id='BitCheckList1' runat='server' />

    But I get the following error message when I run it:

    The base class includes the field 'BitCheckList1', but its type
    (MYPROJ.MyCustomControls.BitCheckList) is not compatible with the type
    of control (MyCustomControls.BitCheckList).

    If I add a Tagname="MyCustomControls" to the register directive, I get a
    different error, namely that the directive lacks a SRC attribute (i.e.
    it is looking for the .ascx file). But I'd like to avoid the
    limitations of user controls if that's possible by inheriting directly
    from the CheckBoxList class. I'd be grateful for explanations of what
    the first error means and how it should be fixed in my code.

    Thanks!
    Timo
    --------
    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.WebControls

    Namespace MyCustomControls
    Public Class BitCheckList
    Inherits System.Web.UI.WebControls.CheckBoxList

    Public Sub New()
    MyBase.new()
    End Sub


    Public Property Bits() As Integer
    Get
    Return GetBits()
    End Get
    Set(ByVal Value As Integer)
    SetBits(Value)
    End Set
    End Property
    Private Function GetBits() As Integer
    Dim LI As System.Web.UI.WebControls.ListItem
    Dim temp As Integer = 0
    For Each LI In Me.Items
    If LI.Selected Then
    temp += LI.Value
    End If
    Next
    Return temp
    End Function
    Private Function SetBits(ByVal v)
    Dim LI As System.Web.UI.WebControls.ListItem
    For Each LI In Me.Items
    LI.Selected = ((v And LI.Value) = LI.Value)
    Next
    End Function

    End Class
     
    Timo, Apr 18, 2004
    #1
    1. Advertising

  2. Timo

    Timo Guest

    Following up on my own question. The problem was caused by the fact that
    the source code for the inherited control was part of the current
    project. The reference to the external DLL doesn't disambiguate things
    at compile time.
    Timo
     
    Timo, Apr 19, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2h1Y2sgSGFlYmVybGU=?=

    Subclassing from System.Web.UI.Page - Designer Error

    =?Utf-8?B?Q2h1Y2sgSGFlYmVybGU=?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    466
    Mike Bridge
    Feb 20, 2004
  2. TheStripe

    Subclassing Controls

    TheStripe, Sep 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    435
    =?Utf-8?B?VGhlU3RyaXBl?=
    Sep 27, 2004
  3. Nathan Sokalski
    Replies:
    5
    Views:
    1,029
    Gaurav Vaish \(www.Edujini-Labs.com\)
    Jan 10, 2007
  4. Nathan Sokalski
    Replies:
    4
    Views:
    304
    Nathan Sokalski
    Dec 21, 2006
  5. Nathan Sokalski
    Replies:
    4
    Views:
    342
    Nathan Sokalski
    Dec 21, 2006
Loading...

Share This Page