User Control Bug when loaded from DLL

M

Matthew

Hi there,

I'm hoping someone has an idea re this problem:

I have a user control which derives from system.web.ui.UserControl and
has a member variable of type
vts.WebControlLibrary.WebControls.ListBox ( which derives from
System.Web.UI.WebControls.ListBox (in order to override the render
contents method of that control):

Codebehind Follows:
-=============================================================================
SelectPlace.ascx.vb
-=============================================================================
Imports System.Web.UI.WebControls
Imports vts.GIS
Namespace WebControls.GISControls
Public Class SelectPlace
Inherits System.Web.UI.UserControl
Private Const Spacer = "  "
Private mThirdPartyID As XKeyType
Private mSearchstring As String


Public Property ThirdPartyID() As XKeyType
Get
Return mThirdPartyID
End Get
Set(ByVal Value As XKeyType)
mThirdPartyID = Value
End Set
End Property

Public Property Searchstring() As String
Get
Return mSearchstring
End Get
Set(ByVal Value As String)
mSearchstring = Value
End Set
End Property
#Region " Web Form Designer Generated Code "


<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub


'NOTE: The following placeholder declaration is required by
the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Protected WithEvents Listbox2 As
vts.WebControlLibrary.WebControls.ListBox

Private Sub Page_Init(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

<B>If Not Listbox2 Is Nothing Then
Listbox2.Items.Clear()
End If</B>

If Me.Searchstring.Trim.Length > 0 Then

Dim oCollMatches As Collection =
Manager.NameSearch(Me.Searchstring)

Dim omatch As PlaceNameMatch
Dim oCollection As New Collection
Dim oColPathToRoot As Collection
Dim pc As ContextTreeNode
Dim arr() As Integer = {CType(Me.ThirdPartyID,
Integer)}
Dim oCTN As ContextTreeNode
Dim item As ListItem
Dim strSpacer As String
Dim i As Integer
Dim spacechar As String = Server.HtmlDecode(Spacer)


For Each omatch In oCollMatches
If CType(omatch.MatchType, Integer) =
PlaceNameMatch.PlaceMatchType.Exact Then
If CType(omatch.PlaceMatch,
Place).PlaceTypeSearch(arr, oCTN) = True Then
oCollection.Add(oCTN)
End If
oCTN = Nothing
End If
Next

For Each pc In oCollection
oColPathToRoot = New Collection
BuildParentPath(pc.Node, oColPathToRoot)
i = oColPathToRoot.Count
strSpacer = ""
For i = oColPathToRoot.Count To 1 Step -1
item = New ListItem
item.Text = strSpacer &
oColPathToRoot.Item(i).placename
item.Attributes.Add("isTypeMatch", "false")
Listbox2.Items.Add(item)
strSpacer = strSpacer & spacechar
Next

item = New ListItem
item.Text = strSpacer & pc.Node.PlaceName
item.Value = pc.Node.PlaceID

SetAttributes(pc, item)

Listbox2.Items.Add(item)
'increment spacer
strSpacer = strSpacer & spacechar
BuildTreeView(pc, strSpacer)
Listbox2.Items.Add("")
Next

End If

End Sub


Private Sub BuildTreeView(ByVal parentnode As ContextTreeNode,
ByVal strSpacer As String)

Dim PC, PCInner As ContextTreeNode
Dim item As ListItem
Dim strOptionText As String
If parentnode.TypeMatchChildNodes.Count > 0 Then
strSpacer = strSpacer & Server.HtmlDecode(Spacer)
End If

For Each PC In parentnode.TypeMatchChildNodes
item = New ListItem
If PC.TypeMatch Then
strOptionText = strSpacer & "(" &
PC.Node.PlaceXKeys.Item(CType(Me.ThirdPartyID,
Integer).ToString).placexkey & ")" & " " & PC.Node.PlaceName
Else
strOptionText = strSpacer & PC.Node.PlaceName
End If

item.Text = strOptionText

SetAttributes(PC, item)

Listbox2.Items.Add(item)
BuildTreeView(PC, strSpacer)
Next

End Sub
Private Sub BuildParentPath(ByVal pl As Place, ByRef coll As
Collection)

pl.LoadParents()
If pl.Parents.Count > 0 Then
coll.Add(pl.Parents.Item(0))
BuildParentPath(pl.Parents.Item(0), coll)
End If
End Sub

Private Sub SetAttributes(ByVal pc As ContextTreeNode, ByRef
lstItem As ListItem)
If pc.TypeMatch Then
lstItem.Attributes.Add("isTypeMatch", "true")
Else
lstItem.Attributes.Add("isTypeMatch", "false")
End If
End Sub

End Class
End Namespace

-=============================================================================
selectplace.ascx

<%@ Register TagPrefix="CustControls"
Namespace="vts.WebControlLibrary.WebControls"
Assembly="vtsWebControlLibrary" %>
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="SelectPlace.ascx.vb"
Inherits="vts.WebControlLibrary.WebControls.GISControls.SelectPlace"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<LINK href="placematch.css" type="text/css" rel="stylesheet">
<CUSTCONTROLS:LISTBOX id="Listbox2" runat="server" Height="248px"
Width="322px" BackColor="LightCyan"
Font-Names="Verdana"></CUSTCONTROLS:LISTBOX>
-=============================================================================


My implemenation of listbox is as follows:

Listbox.vb

-=============================================================================

Imports System

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.ComponentModel

Imports System.Drawing

Imports System.Collections



Namespace WebControls

Public Class ListBox

Inherits System.Web.UI.WebControls.ListBox

Protected Overrides Sub RenderContents(ByVal myWriter As
HtmlTextWriter)

Dim selected As Boolean

Dim isSing As Boolean = False

If (SelectionMode = ListSelectionMode.Single) Then

isSing = True

End If

Dim myLiColl As ListItemCollection = Items

Dim n As Integer = myLiColl.Count

If (n > 0) Then

Dim i As Integer = 0

For i = 0 To n - 1

Dim lItem As ListItem = myLiColl(i)


myWriter.WriteBeginTag("option")

If lItem.Attributes.Item("isTypeMatch") = "true"
Then
myWriter.WriteAttribute("class", "BoldOption")
End If


If (lItem.Selected) Then

If (isSing) Then

If (selected) Then

Throw New Exception("bad")

selected = True

End If

End If

myWriter.WriteAttribute("selected",
"selected")

End If

myWriter.WriteAttribute("value", lItem.Value,
True)

myWriter.Write(HtmlTextWriter.TagRightChar)

myWriter.Write(lItem.Text)

myWriter.WriteEndTag("option")

myWriter.WriteLine()

Next

End If

End Sub

End Class

End Namespace

-=============================================================================


This control works fine when I register it declaratively within the
containing page using the tag / src syntax, however when I try to pull
all the controls out of my web application and into a control
library, reference the associated DLL, and register the control
declaratively using the namespace / assembly syntax, I get the "Object
reference not set to an instance ..." etc error. In other words a null
reference on the listbox ("Listbox2") when it is refenced in
selectplace.ascx.vb

Here is my aspx control test harness

-=============================================================================
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="ControlTest.aspx.vb" Inherits="GISWebClient.ControlTest"%>
<%@ Register TagPrefix="uc1" TagName="SelectPlace"
Src="SelectPlace.ascx" %>
<%@ Register Tagprefix="uc2"
Namespace="vts.WebControlLibrary.WebControls.GISControls"
Assembly="vtsWebControlLibrary" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ControlTest</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<uc2:selectplace id="SelectPlace1"
runat="server"></uc2:selectplace></form>
</body>
</HTML>

-=============================================================================

controltest.aspx.vb

Public Class ControlTest
Inherits System.Web.UI.Page
(NOTE: when i reference the external dll control library, I change the
type of the control here to reflect the correct namespace and
assembly)
Protected WithEvents SelectPlace1 As
GISWebClient.GISControls.SelectPlace
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the
Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object


Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

SelectPlace1.ThirdPartyID = CType(Request.QueryString("type"),
vts.GIS.XKeyType)
SelectPlace1.Searchstring = Request.QueryString("loc")

End Sub

End Class


What is more confusing is that the control renders correctly at design
time..
I'm not sure why this is happening, can someone see my mistake??

I know it may take some time to see what I am trying to do, so any
help would be much appreciated.

Regards and TIA,

Matthew Evans
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top