i have created a simple custom control with a simple text label on it. This is the ascx code for my control.
I have removed the compiler generated stuff to save space.
It has a simple text label on it - called lbBanner
PublicClass BannerAndGrid 'this is my custom control class
Inherits System.Web.UI.UserControl
ProtectedWithEvents lbBannerAs System.Web.UI.WebControls.Label 'this is my text label on my custom control
PrivateSub Page_Init(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.InitInitializeComponent()
EndSub
PublicProperty Banner() ' this property should get/set the text field on the text labale on my custom control
Get
Return lbBanner.Text
EndGet
Set(ByVal Value)
lbBanner.Text = Value'WHEN I DO THIS I GET AN ERROR SAYING THAT THE OBJECT IS NOT SET TO AN INSTANCE OF THE OBJECT
EndSet
EndProperty
EndClass
I instantiate my control thus:
dim aControl as BannerAndGrid
aControl = new BannerAndGrid
aControl.Banner = "abc" ---see above property Banner--when I do this I get a reference error - WHY ?
I cannot see why I cannot set values within my control by using properties to set/get values.
Am I doing something wrong or is there a better way?
All help greatly appreciated.
Cheers.
When you are dynamically creating UserControls you should use LoadControl, rather than instantiating it in the way you are doing. For example:
dim aControl as BannerAndGrid
aControl = CType(LoadControl("/location/of/BannerAndGrid.ascx"), BannerAndGrid)
aControl.Banner = "abc" 'This will work.
Thanks Steven - the above solution worked perfectly.
Cheers
Chas (Canterbury,UK)
0 comments:
Post a Comment