473,320 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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("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
Nov 18 '05 #1
2 1331
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.bludog.net> wrote in message news:gs********************************@4ax.com...
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

Nov 18 '05 #2
On Fri, 28 May 2004 12:30:34 -0400, "Raterus" <ra*****@spam.org>
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
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...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
1
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...
0
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...
5
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...
2
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...
0
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...
5
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...
7
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"; ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.