Monday, March 26, 2012
Trouble firing events in code behind of user control
I am trying to use an asp:linkbutton on a .ascx file (user control), but it does not seem to be firing the click event in the code behind. I have put breakpoints on the first line of the event and in the page load of the user control, but it does not get to any of these. All it does is it postbacks to the server and goes through the page load event of the actual form (.aspx) that the user control is sitting on.
Any ideas?
ThanksAhoy
I am trying to use an asp:linkbutton on a .ascx file (user control), but it does not seem to be firing the click event in the code behind. I have put breakpoints on the first line of the event and in the page load of the user control, but it does not get to any of these. All it does is it postbacks to the server and goes through the page load event of the actual form (.aspx) that the user control is sitting on.
Any ideas?
Thanks
How are you assigning the Click Event to teh LinkButton and how are you handling it?
The following code is placed on the .ascx for the linkbutton's click event
Private Sub lnkLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkLogin.Click
'code
End sub
...it just never goes thru this code
Hmm that should not happen. Are you sure you are running on "Debug" mode and ASP.Net "Debugging" is enabled?
Make sure you are reloading your controls onto the form after the postback, or else the events for them wont fire. Happened to me :wave:
Yeh that was my problem - wasnt reloading the user controls on postback.
thanks patch :thumb:
Saturday, March 24, 2012
trouble setting text values in custom controls
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)