Custom Control Postback Problems

Discussion in 'ASP .Net' started by BluDog, May 28, 2004.

  1. BluDog

    BluDog Guest

    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
     
    BluDog, May 28, 2004
    #1
    1. Advertising

  2. BluDog

    Raterus Guest

    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" <> wrote in message news:...
    > 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
     
    Raterus, May 28, 2004
    #2
    1. Advertising

  3. BluDog

    BluDog Guest

    On Fri, 28 May 2004 12:30:34 -0400, "Raterus" <>
    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
     
    BluDog, May 28, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonah Olsson
    Replies:
    1
    Views:
    610
    Scott Allen
    Apr 5, 2005
  2. J R M
    Replies:
    2
    Views:
    453
    Charlie@CBFC
    Jun 1, 2006
  3. gnewsgroup
    Replies:
    1
    Views:
    399
    gnewsgroup
    Feb 13, 2008
  4. Matt Sokol
    Replies:
    2
    Views:
    426
    Theo Tillotson
    Aug 7, 2003
  5. Amadeus Consulting

    Custom GridView Control – Postback Problems

    Amadeus Consulting, Dec 15, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    388
    Amadeus Consulting
    Dec 19, 2005
Loading...

Share This Page