G
Guest
Hello All:
Do I have to use the LoadControl method of the Page to load a UserControl?
I have a class which contains three methods (one public and two private).
The class acts as a control server. It "serves" back the required control
(either WebControl or UserControl) based on the contents of an xml file. The
code in the webform places each control in a TableCell.
My problem is that the control server works as far as returning the correct
control; but when I view the webform, the UserControl is nowhere to be found.
It's not on the page and not in the View Source.
Here is the server code (I'm not posting GetWebControl here):
Public Function GetControl(ByVal NodeToRender As XmlNode) As
Web.UI.Control
Dim ValueToMatch As String
If NodeToRender.Name.Equals("DisplayText") Then
ValueToMatch = NodeToRender.Name.ToLower
Else
ValueToMatch = NodeToRender.Attributes("type").Value.ToLower
End If
Select Case ValueToMatch.ToLower
Case "label", "textbox", "checkbox", "displaytext"
Return GetWebControl(NodeToRender)
Case "customaddresses"
Return GetUserControl(NodeToRender)
End Select
End Function
Private Function GetUserControl(ByVal NodeToRender As XmlNode) As
Web.UI.UserControl
Dim uControl As New CustomAddresses
uControl.PopulateAddressBoxes(NodeToRender.Attributes("value").Value.Trim)
Return uControl
End Function
Here is the code and HTML for the UserControl (called CustomAddresses):
Public Class CustomAddresses
Inherits System.Web.UI.UserControl
Public Sub New()
Addr1 = New TextBox
Addr2 = New TextBox
Addr3 = New TextBox
Addr4 = New TextBox
End Sub
Public Sub PopulateAddressBoxes(ByVal str As String)
Dim SelectedValue() As String = str.Split(("/").ToCharArray())
Dim Name As String = SelectedValue(0).Trim
Dim Address() As String = SelectedValue(1).Split((",").ToCharArray())
Dim Addr1 As String = Address(0).Trim
Dim Addr2 As String = Address(1).Trim
Dim City As String = Address(Address.Length - 3).Trim
Dim StateProvCd As String = Address(Address.Length - 2).Trim
Dim PostalCode As String = Address(Address.Length - 1).Trim
Me.Addr1.Text = Name
Me.Addr2.Text = Addr1
If Not Addr2.Equals(City) Then
Me.Addr3.Text = Addr2
Me.Addr4.Text = City & " " & StateProvCd & ", " & PostalCode
Else
Me.Addr3.Text = City & " " & StateProvCd & ", " & PostalCode
End If
End Sub
End Class
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="CustomAddresses.ascx.vb" Inherits="WBMI.Claims.CustomAddresses"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<TABLE class="interior" id="Table1" width="100%">
<TR>
<TD class="tabledata">
<asp:TextBox id="Addr1" runat="server" Width="361px" ReadOnly="False"
MaxLength="60"></asp:TextBox></TD>
</TR>
<TR>
<TD class="tabledata">
<asp:TextBox id="Addr2" runat="server" Width="361px" ReadOnly="False"
MaxLength="60"></asp:TextBox></TD>
</TR>
<TR>
<TD class="tabledata">
<asp:TextBox id="Addr3" runat="server" Width="361px" ReadOnly="False"
MaxLength="60"></asp:TextBox></TD>
</TR>
<TR>
<TD class="tabledata">
<asp:TextBox id="Addr4" runat="server" Width="361px" ReadOnly="False"
MaxLength="60"></asp:TextBox></TD>
</TR>
</TABLE>
Here's a sample of the webform code that calls these:
If TypeOf SvrControl.GetControl(m_FormTemplate.GetControlNode(ChildNode)) Is
WebControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetControlNode(ChildNode)),
WebControl)
ElseIf TypeOf
SvrControl.GetControl(m_FormTemplate.GetControlNode(ChildNode)) Is
UserControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetControlNode(ChildNode)),
UserControl)
End If
NewCell.Controls.Add(ControlToRender)
What this boils down to is that I want to create and utilize a ControlServer
class to encapsulate the details about how a control is created and returned.
Can this be done?
TIA,
Do I have to use the LoadControl method of the Page to load a UserControl?
I have a class which contains three methods (one public and two private).
The class acts as a control server. It "serves" back the required control
(either WebControl or UserControl) based on the contents of an xml file. The
code in the webform places each control in a TableCell.
My problem is that the control server works as far as returning the correct
control; but when I view the webform, the UserControl is nowhere to be found.
It's not on the page and not in the View Source.
Here is the server code (I'm not posting GetWebControl here):
Public Function GetControl(ByVal NodeToRender As XmlNode) As
Web.UI.Control
Dim ValueToMatch As String
If NodeToRender.Name.Equals("DisplayText") Then
ValueToMatch = NodeToRender.Name.ToLower
Else
ValueToMatch = NodeToRender.Attributes("type").Value.ToLower
End If
Select Case ValueToMatch.ToLower
Case "label", "textbox", "checkbox", "displaytext"
Return GetWebControl(NodeToRender)
Case "customaddresses"
Return GetUserControl(NodeToRender)
End Select
End Function
Private Function GetUserControl(ByVal NodeToRender As XmlNode) As
Web.UI.UserControl
Dim uControl As New CustomAddresses
uControl.PopulateAddressBoxes(NodeToRender.Attributes("value").Value.Trim)
Return uControl
End Function
Here is the code and HTML for the UserControl (called CustomAddresses):
Public Class CustomAddresses
Inherits System.Web.UI.UserControl
Public Sub New()
Addr1 = New TextBox
Addr2 = New TextBox
Addr3 = New TextBox
Addr4 = New TextBox
End Sub
Public Sub PopulateAddressBoxes(ByVal str As String)
Dim SelectedValue() As String = str.Split(("/").ToCharArray())
Dim Name As String = SelectedValue(0).Trim
Dim Address() As String = SelectedValue(1).Split((",").ToCharArray())
Dim Addr1 As String = Address(0).Trim
Dim Addr2 As String = Address(1).Trim
Dim City As String = Address(Address.Length - 3).Trim
Dim StateProvCd As String = Address(Address.Length - 2).Trim
Dim PostalCode As String = Address(Address.Length - 1).Trim
Me.Addr1.Text = Name
Me.Addr2.Text = Addr1
If Not Addr2.Equals(City) Then
Me.Addr3.Text = Addr2
Me.Addr4.Text = City & " " & StateProvCd & ", " & PostalCode
Else
Me.Addr3.Text = City & " " & StateProvCd & ", " & PostalCode
End If
End Sub
End Class
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="CustomAddresses.ascx.vb" Inherits="WBMI.Claims.CustomAddresses"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<TABLE class="interior" id="Table1" width="100%">
<TR>
<TD class="tabledata">
<asp:TextBox id="Addr1" runat="server" Width="361px" ReadOnly="False"
MaxLength="60"></asp:TextBox></TD>
</TR>
<TR>
<TD class="tabledata">
<asp:TextBox id="Addr2" runat="server" Width="361px" ReadOnly="False"
MaxLength="60"></asp:TextBox></TD>
</TR>
<TR>
<TD class="tabledata">
<asp:TextBox id="Addr3" runat="server" Width="361px" ReadOnly="False"
MaxLength="60"></asp:TextBox></TD>
</TR>
<TR>
<TD class="tabledata">
<asp:TextBox id="Addr4" runat="server" Width="361px" ReadOnly="False"
MaxLength="60"></asp:TextBox></TD>
</TR>
</TABLE>
Here's a sample of the webform code that calls these:
If TypeOf SvrControl.GetControl(m_FormTemplate.GetControlNode(ChildNode)) Is
WebControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetControlNode(ChildNode)),
WebControl)
ElseIf TypeOf
SvrControl.GetControl(m_FormTemplate.GetControlNode(ChildNode)) Is
UserControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetControlNode(ChildNode)),
UserControl)
End If
NewCell.Controls.Add(ControlToRender)
What this boils down to is that I want to create and utilize a ControlServer
class to encapsulate the details about how a control is created and returned.
Can this be done?
TIA,