T
Timo
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
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