使用New Contructor加载UserControl [英] Loading UserControl using New Contructor

查看:60
本文介绍了使用New Contructor加载UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:


我是否必须使用Page的LoadControl方法加载UserControl?


我有课其中包含三种方法(一种是公共方式,另一种是私有方式)。

该类充当控制服务器。它服务根据xml文件的内容返回所需的控件

(WebControl或UserControl)。 webform中的

代码将每个控件放在一个TableCell中。


我的问题是控制服务器的工作原理是返回正确的

控制;但是当我查看webform时,UserControl无处可寻。

它不在页面上而不在View Source中。


这是服务器代码(我不是在这里发布GetWebControl):


公共函数GetControl(ByVal NodeToRender作为XmlNode)作为

Web.UI.控制

Dim ValueToMatch As String

如果NodeToRender.Name.Equals(" DisplayText")那么

ValueToMatch = NodeToRender.Name.ToLower

Else

ValueToMatch = NodeToRender.Attributes(" type")。Value.ToLower

结束如果

选择Case ValueToMatch.ToLower

Case" label"," textbox"," checkbox"," displaytext"

返回GetWebControl(NodeToRender)


案例" customaddresses"

返回GetUserControl(NodeToRender)


结束选择

结束功能


私有函数GetUserControl(ByVal No deToRender As XmlNode)As

Web.UI.UserControl

Dim uControl As New CustomAddresses


uControl.PopulateAddressBoxes(NodeToRender.Attribu tes(value)。Value.Trim)

返回uControl

结束函数


这是代码和HTML对于UserControl(称为CustomAddresses):


公共类CustomAddresses

继承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=地址(1).Trim

Dim City As String =地址(Address.Length - 3).Trim

Dim StateProvCd As String = Address(Address.Length - 2 ).Trim

Dim PostalCode As String =地址(Address.Length - 1).Trim


Me.Addr1.Text =姓名

Me.Addr2.Text = Addr1

如果不是Addr2.Equals(City)那么

Me.Addr3.Text = Addr2

Me.Addr4.Text = City& " " &安培; StateProvCd& , &安培; PostalCode

Else

Me.Addr3.Text = City& " " &安培; StateProvCd& , &安培; PostalCode

结束如果

结束子

结束班


<%@ Control Language = " VB" AutoEventWireup =" false"
Codebehind =" CustomAddresses.ascx.vb" Inherits =" WBMI.Claims.CustomAddresses"

TargetSchema =" http://schemas.microsoft.com/intellisense/ie5"%>

< TABLE类= QUOT;室内" ID = QUOT;表1" width =" 100%">

< TR>

< TD class =" tabledata">

< ; asp:TextBox id =" Addr1" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"

MaxLength =" 60">< / asp:TextBox>< / TD>

< / TR>

< TR>

< TD class =" tabledata">

< asp:TextBox id =" Addr2" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"

MaxLength =" 60">< / asp:TextBox>< / TD>

< / TR>

< TR>

< TD class =" tabledata">

< asp:TextBox id =" Addr3" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"

MaxLength =" 60">< / asp:TextBox>< / TD>

< / TR>

< TR>

< TD class =" tabledata">

< asp:TextBox id =" Addr4" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"

MaxLength =" 60">< / asp:TextBox>< / TD>

< / TR>

< / TABLE>


这里是调用这些代码的webform代码示例:


如果TypeOf SvrControl .GetControl(m_FormTemplate.GetControlNod e(ChildNode))是

WebControl然后

ControlToRender =

CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode) (ChildNode)),

WebControl)


ElseIf TypeOf

SvrControl.GetControl(m_FormTemplate.GetControlNod e(ChildNode))是

UserControl然后

ControlToRender =

CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),

UserControl)


结束如果

NewCell.Controls.Add(ControlToRender)


这归结为什么是我想创建和利用ControlServer

类来封装控件如何创建的详细信息已经退回。


可以这样做吗?


TIA,

-

Joe


VB.NET/C#/ASP.NET/ASP/VB/C++/Web和数据库开发/ VBA自动化

解决方案

我发现我必须覆盖UserControl中的Render方法。


我真的不懂HtmlTextWriter。如何将填充的

TextBox控件写入webform?


感谢您提供任何可用的帮助。

-

Joe


VB.NET/C#/ASP.NET/ASP/VB/C++/Web和数据库开发/ VBA自动化

"乔"写道:

大家好:

我是否必须使用Page的LoadControl方法加载UserControl?

我有一个类包含三个方法(一个公共和两个私有)。
该类充当控制服务器。它服务根据xml文件的内容返回所需的控件(WebControl或UserControl)。 webform中的
代码将每个控件放在一个TableCell中。

我的问题是控制服务器的工作方式就是返回正确的
控件;但是当我查看webform时,UserControl无处可寻。
它不在页面上而不在View Source中。

这是服务器代码(我'我没有在这里发布GetWebControl):公共函数GetControl(ByVal NodeToRender作为XmlNode)作为
Web.UI.Control
Dim ValueToMatch As String
如果是NodeToRender。 Name.Equals(" DisplayText")然后
ValueToMatch = NodeToRender.Name.ToLower
Else
ValueToMatch = NodeToRender.Attributes(" type")。Value.ToLower
End如果选择Case ValueToMatch.ToLower
Case" label",textbox," checkbox",displaytext
返回GetWebControl(NodeToRender)

>案例" customaddresses"
返回GetUserControl(NodeToRender)

结束选择
结束功能

>私有函数GetUserControl(ByVal NodeToRender作为XmlNode)作为Web.UI.UserControl
Dim uControl作为新的CustomAddresses

uControl.PopulateAddressBoxes(NodeToRender.Attribu tes(" value" ).Value.Trim)
返回uControl
结束函数

以下是UserControl的代码和HTML(称为CustomAddresses):

公共类CustomAddresses
继承System.Web.UI.UserControl

Public Sub New()
Addr1 = New TextBox
Addr2 = New TextBox
Addr3 = New TextBox
Addr4 =新TextBox
End Sub
公共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 = A.地址(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
如果不是Addr2.Equals(City)那么
Me.Addr3.Text = Addr2
Me.Addr4.Text = City& " " &安培; StateProvCd& , &安培; PostalCode
Else
Me.Addr3.Text = City& " " &安培; StateProvCd& , &安培; PostalCode
结束如果
End Sub


<%@ Control Language =" vb" AutoEventWireup =" false"
Codebehind =" CustomAddresses.ascx.vb" Inherits =" WBMI.Claims.CustomAddresses"
TargetSchema =" http://schemas.microsoft.com/intellisense/ie5"%>
< TABLE class =" interior" ID = QUOT;表1" width =" 100%">
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr1" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr2" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr3" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr4" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< / TABLE>

以下是调用这些代码的webform代码示例:

如果TypeOf SvrControl.GetControl(m_FormTemplate.GetControlNod e(ChildNode))是
WebControl那么
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),
WebControl)

ElseIf TypeOf
SvrControl.GetControl(m_FormTemplate.GetControlNod e(ChildNode))是
UserControl然后
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),
UserControl)
结束如果
NewCell.Controls.Add(ControlToRender)

这归结为我想创建并利用ControlServer
类来封装有关如何控制被创建并返回。

这可以吗?

TIA,
-

VB.NET/C#/ASP.NET/ASP/VB/C++/Web和数据库开发/ VBA自动化



你能澄清更多为什么你不想调用LoadControl加载

控件。 LoadControl存在的原因是ASP.NET具有准备对象所需的能力,例如加载ASCX和创建对象模型

。如果您使用构造函数创建一个,那么您可以阻止

引擎执行它想要的操作。


所有控件都有Render方法,但是你不应该在

usercontrol中覆盖它。有一些自定义Web控件从WebControl继承

然后你可以覆盖Render方法。


所有HtmlTextWriter都允许你输出HTML。一个文本框是

只不过< input type =" text" value =" something" /> ;.

" Joe" <乔****** @ donotspam.yahoo.com>在消息中写道

新闻:0F ********************************** @ microsof t.com ...

我发现我必须覆盖UserControl中的Render方法。

我真的不了解HtmlTextWriter。如何将填充的
TextBox控件写入webform?

感谢您提供任何可用的帮助。
-
Joe

VB .NET / C#/ ASP.NET / ASP / VB / C ++ / Web和DB开发/ VBA自动化

" Joe"写道:

大家好:

我是否必须使用Page的LoadControl方法加载
UserControl?
<我有一个类,它包含三个方法(一个公共和两个私有)。
该类充当控制服务器。它服务根据xml文件的内容返回所需的控件
(WebControl或UserControl)。
webform中的代码将每个控件放在一个TableCell中。

我的问题是控制服务器的工作原理是返回
正确的控制;但是当我查看webform时,无法找到UserControl。
它不在页面上而不在View Source中。

这是服务器代码(我不是在这里发布GetWebControl):

公共函数GetControl(ByVal NodeToRender作为XmlNode)As
Web.UI.Control
Dim ValueToMatch As String
如果NodeToRender.Name.Equals(" DisplayText")那么
ValueToMatch = NodeToRender.Name.ToLower
Else
ValueToMatch = NodeToRender.Attributes(" type")。Value.ToLower
结束如果
Select Case ValueToMatch.ToLower
案例标签,文本框,复选框,displaytext
返回GetWebControl(NodeToRender)

Case" customaddresses"
返回GetUserControl(NodeToRender)

结束选择
结束功能
私有函数GetUserControl(ByVal NodeToRender作为XmlNode)作为
Web.UI.UserControl
Dim uControl作为新的CustomAddresses

uControl.PopulateAddressBoxes(NodeToRender.Attribu tes( " value")。Value.Trim)
返回uControl
结束函数

以下是UserControl的代码和HTML(称为CustomAddresses):
继承System.Web.UI.UserControl

Public Sub New()
Addr1 = New TextBox
Addr2 = New TextBox
Addr3 = New TextBox
Addr4 = New TextBox
End Sub
公共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 A. s 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
如果不是Addr2.Equals(City)那么
Me.Addr3.Text = Addr2
Me.Addr4.Text = City& " " &安培; StateProvCd& , &安培; PostalCode
Else
Me.Addr3.Text = City& " " &安培; StateProvCd& , &安培; PostalCode
结束如果
End Sub


<%@ Control Language =" vb" AutoEventWireup =" false"
Codebehind =" CustomAddresses.ascx.vb"
Inherits =" WBMI.Claims.CustomAddresses"
TargetSchema =" http://schemas.microsoft。 com / intellisense / ie5"%>
< TABLE class =" interior" ID = QUOT;表1" width =" 100%">
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr1" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr2" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr3" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr4" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< / TABLE>

以下是调用这些代码的webform代码示例:

如果TypeOf SvrControl.GetControl(m_FormTemplate.GetControlNod e(ChildNode))

WebControl然后
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),
WebControl)

ElseIf TypeOf
SvrControl.GetControl (m_FormTemplate.GetControlNod e(ChildNode))是
UserControl然后
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),
UserControl)

结束如果
NewCell.Controls.Add(ControlToRender)

这归结为我想要创建和使用
ControlServer
class来封装有关如何创建控件和返回的详细信息。

可以这样做吗?

TIA,
-
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web和数据库开发/ VBA自动化



嗨Peter,


我们正在使用xml文档来驱动网页的创建。

文档定义了要在webform上包含哪些控件以及

控件将出现在哪里。


我有很多定义文档结构的方式以及

使用。


我的想法是有一个类来封装所需的生成/>
控件(无论是WebControl还是UserControl)并将其返回到webform。

在这个应用程序中,UserControl只是一组WebControls。例如,

一个UserControl(CustomAddresses)是四个文本框服务器控件组合

一起用于name,address1,address2和city-state-zip。这将在页面上显示一个人的地址时使用




这是否有助于你理解我正在尝试做的帽子?

-

Joe


VB.NET/C#/ASP.NET/ASP/VB/C++/Web和DB开发/ VBA自动化

" Peter Rilling"写道:

你能否澄清更多为什么你不想调用LoadControl来加载
控件。 LoadControl存在的原因是ASP.NET具有准备对象所需的能力,例如加载ASCX和创建对象模型。如果您使用构造函数创建一个,那么您可以阻止
引擎执行它想要的操作。

所有控件都有Render方法,但是您不应该在 usercontrol。有一些自定义Web控件从WebControl继承,然后您可以覆盖Render方法。

所有HtmlTextWriter都允许您输出HTML。文本框只不过是< input type =" text" value =" something" /> ;.

" Joe" <乔****** @ donotspam.yahoo.com>在消息中写道
新闻:0F ********************************** @ microsof t.com。 ..

我发现我必须覆盖UserControl中的Render方法。

我真的不了解HtmlTextWriter。如何将填充的
TextBox控件写入webform?

感谢您提供任何可用的帮助。
-
Joe

VB .NET / C#/ ASP.NET / ASP / VB / C ++ / Web和DB开发/ VBA自动化

" Joe"写道:

大家好:

我是否必须使用Page的LoadControl方法加载
UserControl?
<我有一个类,它包含三个方法(一个公共和两个私有)。
该类充当控制服务器。它服务根据xml文件的内容返回所需的控件
(WebControl或UserControl)。
webform中的代码将每个控件放在一个TableCell中。

我的问题是控制服务器的工作原理是返回
正确的控制;但是当我查看webform时,无法找到UserControl。
它不在页面上而不在View Source中。

这是服务器代码(我不是在这里发布GetWebControl):

公共函数GetControl(ByVal NodeToRender作为XmlNode)As
Web.UI.Control
Dim ValueToMatch As String
如果NodeToRender.Name.Equals(" DisplayText")那么
ValueToMatch = NodeToRender.Name.ToLower
Else
ValueToMatch = NodeToRender.Attributes(" type")。Value.ToLower
结束如果
Select Case ValueToMatch.ToLower
案例标签,文本框,复选框,displaytext
返回GetWebControl(NodeToRender)

Case" customaddresses"
返回GetUserControl(NodeToRender)

结束选择
结束功能
私有函数GetUserControl(ByVal NodeToRender作为XmlNode)作为
Web.UI.UserControl
Dim uControl作为新的CustomAddresses

uControl.PopulateAddressBoxes(NodeToRender.Attribu tes( " value")。Value.Trim)
返回uControl
结束函数

以下是UserControl的代码和HTML(称为CustomAddresses):
继承System.Web.UI.UserControl

Public Sub New()
Addr1 = New TextBox
Addr2 = New TextBox
Addr3 = New TextBox
Addr4 = New TextBox
End Sub
公共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 A. s 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
如果不是Addr2.Equals(City)那么
Me.Addr3.Text = Addr2
Me.Addr4.Text = City& " " &安培; StateProvCd& , &安培; PostalCode
Else
Me.Addr3.Text = City& " " &安培; StateProvCd& , &安培; PostalCode
结束如果
End Sub


<%@ Control Language =" vb" AutoEventWireup =" false"
Codebehind =" CustomAddresses.ascx.vb"
Inherits =" WBMI.Claims.CustomAddresses"
TargetSchema =" http://schemas.microsoft。 com / intellisense / ie5"%>
< TABLE class =" interior" ID = QUOT;表1" width =" 100%">
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr1" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr2" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr3" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< TR>
< TD class =" tabledata">
< asp:TextBox id =" Addr4" RUNAT = QUOT;服务器"宽度= QUOT; 361px" ReadOnly =" False"
MaxLength =" 60">< / asp:TextBox>< / TD>
< / TR>
< / TABLE>

以下是调用这些代码的webform代码示例:

如果TypeOf SvrControl.GetControl(m_FormTemplate.GetControlNod e(ChildNode))

WebControl然后
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),
WebControl)

ElseIf TypeOf
SvrControl.GetControl (m_FormTemplate.GetControlNod e(ChildNode))是
UserControl然后
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),
UserControl)

结束如果
NewCell.Controls.Add(ControlToRender)

这归结为我想要创建和使用
ControlServer
class来封装有关如何创建控件和返回的详细信息。

可以这样做吗?

TIA,
-
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web和数据库开发/ VBA自动化




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.Attribu tes("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.GetControlNod e(ChildNode)) Is
WebControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),
WebControl)

ElseIf TypeOf
SvrControl.GetControl(m_FormTemplate.GetControlNod e(ChildNode)) Is
UserControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(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,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation

解决方案

I figured out that I have to override the Render method in the UserControl.

I do not really understand the HtmlTextWriter. How do I write populated
TextBox controls to the webform?

Thank you for any available help.
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
"Joe" wrote:

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.Attribu tes("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.GetControlNod e(ChildNode)) Is
WebControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),
WebControl)

ElseIf TypeOf
SvrControl.GetControl(m_FormTemplate.GetControlNod e(ChildNode)) Is
UserControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(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,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation



Can you clarify more why you don''t want to call LoadControl to load the
control. The reason LoadControl exists is so that ASP.NET has the ability
to prepare the object has it expects such as loading the ASCX and creating
an object model. If you create one with a constructor, then you prevent the
engine from doing what it was suppose to.

All controls have the Render method, but you should not override that in a
usercontrol. There are such things as custom web controls which inherit
from WebControl which you can then override the Render method.

All the HtmlTextWriter does is allow you to output the HTML. A textbox is
nothing more than <input type="text" value="something"/>.
"Joe" <jo******@donotspam.yahoo.com> wrote in message
news:0F**********************************@microsof t.com...

I figured out that I have to override the Render method in the UserControl.

I do not really understand the HtmlTextWriter. How do I write populated
TextBox controls to the webform?

Thank you for any available help.
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
"Joe" wrote:

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.Attribu tes("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.GetControlNod e(ChildNode))
Is
WebControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),
WebControl)

ElseIf TypeOf
SvrControl.GetControl(m_FormTemplate.GetControlNod e(ChildNode)) Is
UserControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(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,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation



Hi Peter,

We are using an xml document to "drive" the creation of web pages. The
document defines what controls to include on a webform and where where the
controls will appear.

I have alot of lead-way in defining how the document will be structured and
used.

My idea was to have a class to encapsulate the generation of the required
control (be it a WebControl or a UserControl) and returns it to the webform.
In this app, a UserControl is just a grouping of WebControls. For example,
one UserControl (CustomAddresses) is four textbox server controls grouped
together for name, address1, address2 and city-state-zip. This will be used
on the page when it will display a person''s addresses.

Does this help you understand hat I am really trying to do?
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
"Peter Rilling" wrote:

Can you clarify more why you don''t want to call LoadControl to load the
control. The reason LoadControl exists is so that ASP.NET has the ability
to prepare the object has it expects such as loading the ASCX and creating
an object model. If you create one with a constructor, then you prevent the
engine from doing what it was suppose to.

All controls have the Render method, but you should not override that in a
usercontrol. There are such things as custom web controls which inherit
from WebControl which you can then override the Render method.

All the HtmlTextWriter does is allow you to output the HTML. A textbox is
nothing more than <input type="text" value="something"/>.
"Joe" <jo******@donotspam.yahoo.com> wrote in message
news:0F**********************************@microsof t.com...

I figured out that I have to override the Render method in the UserControl.

I do not really understand the HtmlTextWriter. How do I write populated
TextBox controls to the webform?

Thank you for any available help.
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
"Joe" wrote:

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.Attribu tes("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.GetControlNod e(ChildNode))
Is
WebControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(ChildNode)),
WebControl)

ElseIf TypeOf
SvrControl.GetControl(m_FormTemplate.GetControlNod e(ChildNode)) Is
UserControl Then
ControlToRender =
CType(SvrControl.GetControl(m_FormTemplate.GetCont rolNode(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,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation




这篇关于使用New Contructor加载UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆