R
rn5a
Suppose I have this property in a user control (which is named
Address.ascx):
<script runat="server">
Public Property Caption() As Object
Get
Caption = lblCaption.Text
End Get
Set(ByVal value As Object)
lblCaption.Text = value
End Set
End Property
</script>
<asp:Label ID="lblCaption" runat="server"/>
When I try to use the above property in an ASPX page with the following
code:
<%@ Register TagPrefix="NConnect" TagName="Address" Src="Address.ascx"
%>
<script runat="server">
Sub Page_Load(.....)
CType(ncBilling.Caption, Label).Text = "BILLING ADDRESS"
CType(ncShipping.Caption, Label).Text = "SHIPPING ADDRESS"
End Sub
</script>
<form runat="server">
<NConnect:Address ID="ncBilling" runat="server"/><br>
<NConnect:Address ID="ncShipping" runat="server"/>
</form>
then ASP.NET generates the error:
Unable to cast object of type 'System.String' to type
'System.Web.UI.WebControls.Label'.
pointing to the first line in the Page_Load sub.
Can someone please explain me what am I doing wrong here?
Please note that I am very much aware that if the property type in
Address.ascx is changed to String, then in the ASPX page
ncBilling.Caption = "BILLING ADDRESS"
ncShipping.Caption = "SHIPPING ADDRESS"
will resolve the issue but I would like to know why does the error get
generated if I opt for the former code (when the type of the property
named Caption is set to Object & not to String)?
Address.ascx):
<script runat="server">
Public Property Caption() As Object
Get
Caption = lblCaption.Text
End Get
Set(ByVal value As Object)
lblCaption.Text = value
End Set
End Property
</script>
<asp:Label ID="lblCaption" runat="server"/>
When I try to use the above property in an ASPX page with the following
code:
<%@ Register TagPrefix="NConnect" TagName="Address" Src="Address.ascx"
%>
<script runat="server">
Sub Page_Load(.....)
CType(ncBilling.Caption, Label).Text = "BILLING ADDRESS"
CType(ncShipping.Caption, Label).Text = "SHIPPING ADDRESS"
End Sub
</script>
<form runat="server">
<NConnect:Address ID="ncBilling" runat="server"/><br>
<NConnect:Address ID="ncShipping" runat="server"/>
</form>
then ASP.NET generates the error:
Unable to cast object of type 'System.String' to type
'System.Web.UI.WebControls.Label'.
pointing to the first line in the Page_Load sub.
Can someone please explain me what am I doing wrong here?
Please note that I am very much aware that if the property type in
Address.ascx is changed to String, then in the ASPX page
ncBilling.Caption = "BILLING ADDRESS"
ncShipping.Caption = "SHIPPING ADDRESS"
will resolve the issue but I would like to know why does the error get
generated if I opt for the former code (when the type of the property
named Caption is set to Object & not to String)?