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="NCon nect" TagName="Addres s" Src="Address.as cx"
%>
<script runat="server">
Sub Page_Load(..... )
CType(ncBilling .Caption, Label).Text = "BILLING ADDRESS"
CType(ncShippin g.Caption, Label).Text = "SHIPPING ADDRESS"
End Sub
</script>
<form runat="server">
<NConnect:Addre ss ID="ncBilling" runat="server"/><br>
<NConnect:Addre ss 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.Lab el'.
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.Capti on = "BILLING ADDRESS"
ncShipping.Capt ion = "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)?