Custom Control Postback Problems

B

BluDog

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("Images") Is Nothing Then
ViewState("Images") = New ImageCollection
End if
Return CType(ViewState("Images"), GalleryImageCollection)
End Get
Set(ByVal Value As ImageCollection)
ViewState("Images") = Value
End Set
End Property

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

#End Region

#Region "Initilization"

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender

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

AddImages()

End Sub

Private Sub AddImages

Dim Image as WebControls.Image
Dim ImageCounter as Integer
For Each Image in Images
Dim ImageButton as New WebControls.ImageButton
ImageButton.ImageUrl = Image.ImageUrl
ImageButton.ID = "Image" & ImageCounter.ToString
Me.Controls.Add(ImageButton)
AddHandler ImageButton.Click, AddressOf Image_Click
ImageCounter += 1
Next

End Sub

#End Region

#Region "Implementation"

Private Sub Image_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs)

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.Click 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
 
R

Raterus

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
 
B

BluDog

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top