473,785 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom Control Postback Problems

Hi

I have a created a custom web control called ImageBrowser, extract is
below:

<Code>

#Region "Properties "

Public Property Images() As ImageCollection
Get
If ViewState("Imag es") Is Nothing Then
ViewState("Imag es") = New ImageCollection
End if
Return CType(ViewState ("Images"), GalleryImageCol lection)
End Get
Set(ByVal Value As ImageCollection )
ViewState("Imag es") = Value
End Set
End Property

Public Property CurrentImage() As Integer
Get
Return CInt(ViewState( "CurrentImage") )
End Get
Set(ByVal Value As Integer)
ViewState("Curr entImage") = Value
End Set
End Property

#End Region

#Region "Initilizat ion"

Private Sub Page_PreRender( ByVal sender As Object, ByVal e As
System.EventArg s) Handles MyBase.PreRende r

If Not IsPostBack Then GetData() 'populates Images
collection from database

AddImages()

End Sub

Private Sub AddImages

Dim Image as WebControls.Ima ge
Dim ImageCounter as Integer
For Each Image in Images
Dim ImageButton as New WebControls.Ima geButton
ImageButton.Ima geUrl = Image.ImageUrl
ImageButton.ID = "Image" & ImageCounter.To String
Me.Controls.Add (ImageButton)
AddHandler ImageButton.Cli ck, AddressOf Image_Click
ImageCounter += 1
Next

End Sub

#End Region

#Region "Implementation "

Private Sub Image_Click(ByV al sender As Object, ByVal e As
System.Web.UI.I mageClickEventA rgs)

Dim ImageName As String = CType(sender, ImageButton).ID
CurrentImage = CInt(ImageName. Substring(5))

End Sub

#End Region

</Code>

Within the AddImages function each image in the Images collection is
added to the control as an ImageButton, I have to do this in the
prerender because i need to know what the CurrentImage property is
from the ViewState and this in the first place i have found it to be
populated.

The only problem is that the event for the ImageButton.Cli ck is not
firing, i believe this is because i have added it to late in the page
life cycle. This appears to be a but of a nasty circle. Does anyone
know where i am going wrong?

Thanks

Blu
Nov 18 '05 #1
2 1357
Eww..

Yeah I think you know your problem already, you need to call AddImages from page_load and not PreRender. By PreRender it is one step too late to process Handlers, as that step just passed.

Here is a good article on the ASP.NET page lifecycle, it may help understanding when viewstate gets populated, I don't see why you couldn't do it in page_load, perhaps you could share your problems when you try that next.

http://www.15seconds.com/Issue/020102.htm

Happy Coding!
--Michael
"BluDog" <ne**@nospam.bl udog.net> wrote in message news:gs******** *************** *********@4ax.c om...
Hi

I have a created a custom web control called ImageBrowser, extract is
below:

<Code>

#Region "Properties "

Public Property Images() As ImageCollection
Get
If ViewState("Imag es") Is Nothing Then
ViewState("Imag es") = New ImageCollection
End if
Return CType(ViewState ("Images"), GalleryImageCol lection)
End Get
Set(ByVal Value As ImageCollection )
ViewState("Imag es") = Value
End Set
End Property

Public Property CurrentImage() As Integer
Get
Return CInt(ViewState( "CurrentImage") )
End Get
Set(ByVal Value As Integer)
ViewState("Curr entImage") = Value
End Set
End Property

#End Region

#Region "Initilizat ion"

Private Sub Page_PreRender( ByVal sender As Object, ByVal e As
System.EventArg s) Handles MyBase.PreRende r

If Not IsPostBack Then GetData() 'populates Images
collection from database

AddImages()

End Sub

Private Sub AddImages

Dim Image as WebControls.Ima ge
Dim ImageCounter as Integer
For Each Image in Images
Dim ImageButton as New WebControls.Ima geButton
ImageButton.Ima geUrl = Image.ImageUrl
ImageButton.ID = "Image" & ImageCounter.To String
Me.Controls.Add (ImageButton)
AddHandler ImageButton.Cli ck, AddressOf Image_Click
ImageCounter += 1
Next

End Sub

#End Region

#Region "Implementation "

Private Sub Image_Click(ByV al sender As Object, ByVal e As
System.Web.UI.I mageClickEventA rgs)

Dim ImageName As String = CType(sender, ImageButton).ID
CurrentImage = CInt(ImageName. Substring(5))

End Sub

#End Region

</Code>

Within the AddImages function each image in the Images collection is
added to the control as an ImageButton, I have to do this in the
prerender because i need to know what the CurrentImage property is
from the ViewState and this in the first place i have found it to be
populated.

The only problem is that the event for the ImageButton.Cli ck is not
firing, i believe this is because i have added it to late in the page
life cycle. This appears to be a but of a nasty circle. Does anyone
know where i am going wrong?

Thanks

Blu

Nov 18 '05 #2
On Fri, 28 May 2004 12:30:34 -0400, "Raterus" <ra*****@spam.o rg>
wrote:
Eww..

Yeah I think you know your problem already, you need to call AddImages from page_load and not PreRender. By PreRender it is one step too late to process Handlers, as that step just passed.

Here is a good article on the ASP.NET page lifecycle, it may help understanding when viewstate gets populated, I don't see why you couldn't do it in page_load, perhaps you could share your problems when you try that next.

http://www.15seconds.com/Issue/020102.htm

Happy Coding!
--Michael


Michael

Thanks for your comments.

I originally had the add images in the OnLoad, however at this point
the postback information has not been populated. For example i have a
button on the form that moves to the next image, to do this the
CurrentImage property is incremented in the Button.Click event
handler.

This property is not available with the updated value until the
PreRender event. I know the PreRender is too late for adding event
handlers for the dynamically created ImageButtons, but the Page_Load
is too early to get the ViewState properties.

I know i am missing the point somewhere but been suffering these sorts
of problems since trying to take advantage of postbacks rather than
the querystring approach.

The article you pointed me to is very interesting, i actually refered
to it extensively prior to my original posting, however it does not
relate to custom web controls.

Thanks

Blu

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1980
by: Lamont Adams | last post by:
Hi all, I've created numerous custom controls of varying complexity, but I've been on this problem for a day and a half, and I can't figure this mystery out. I hope one of you kind folks can point out the really obvious and stupid thing I'm overlooking here. :) I have a custom control that provides a tasklist similar to what you get in certain parts of Office. On the client it consists of a bunch of nested, named and id'ed divs with...
7
3019
by: Shimon Sim | last post by:
I have a custom composite control I have following property
1
4320
by: Steve Loper | last post by:
I am developing a control in ASP.NET 2.0 that is derived from GridView. This control handles paging on its own and thus renders custom paging buttons. It also add some other functionality to the GridView by overriding Render() and rendering other controls alongside the GridView’s HTML. When I click one of the pager buttons (of type ImageButton), I receive the following error: Invalid postback or callback argument. Event validation...
0
1336
by: Iain | last post by:
Can I apologise for the lengthy nature of this post. The scenario is complicated (though I hope the solution is not!) basically, I've got a custom template control which binds itself to a tree structure. The template items describe how each particular note type of the tree should be rendered (the nodes are data capture elements - bool string and more complicated things). I don't want to use ViewState because of size (there are...
5
2347
by: Alan Silver | last post by:
Hello, I have a products page that takes a product ID in the query string. Based on the product details (from a database), the page then loads up one of a number of custom controls, calls a method in the control (that displays product info) and then loads the control into a placeholder so it will be displayed. I want to be able to restore the state of the controls in the custom control, but can't get it to work. For example, the...
2
1880
by: J R M | last post by:
I've developed a custom control (it's basically a drop-down list and then a couple of textboxes to include meta-data for the selection) that I'm embedding into another custom control (the idea is to allow a dynamic number of control1 to appear in control2). Everything renders correctly, however I could never retrieve data on postback from my embedded controls. Upon closer inspection, all of the embedded controls output without any "name"...
0
1657
by: =?Utf-8?B?TWFyayBHaWxrZXM=?= | last post by:
I have designed and written a custom menu like server control. The control inherits from DataBoundControl and implements INamingContainer and IPostBackEventHandler interfaces. The control uses only html mark-up as it’s UI, i.e. tables, table rows and table cells, and no intrinsic html controls such as input or select. The control does not implement any client-side script and therefore redraws its updated UI on postback through the...
5
1420
by: jbeckh2 | last post by:
Hi, I am trying to create a custom server control that contains several dropdownlists. The control looks like this: Dim pnlHeader As New Panel Protected Overrides Sub RenderContents(ByVal writer as HtmlTextWriter) pnlHeader.RenderControl(writer) End Sub
7
3090
by: adiel_g | last post by:
Hello Everyone, I created a custom control. On the CreateChildControls, I added a textbox to the control as follows: // TextBox TextBox txtValue = new TextBox(); txtValue.ID = "txtValue"; protected override void CreateChildControls() { // Add the control to the form
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.