Dynamically added controls based on information from viewstate

Discussion in 'ASP .Net Web Controls' started by Ross Presser, Feb 25, 2008.

  1. Ross Presser

    Ross Presser Guest

    My problem is probably very similar to the frequently asked questions
    about dynamically added controls, but I can't quite resolve it.
    Probably I don't understand the page lifecycle well enough.

    The goal is to have a custom Web Control that has a particular object
    property (called PricingModel). When the property is not set, the
    control has a default appearance (showing "N/A" for some fields). When
    the property is set, it uses information in the control to determine
    how many dropdownlist child controls to add and what their choices
    are. Once that's done, the values of those dropdownlists that are
    chosen by the end web user are used to compute a couple of values,
    which are accessible to the parent page through another object
    property (called PricingResult)

    Currently I am storing the value of the PricingModel property in
    ViewState immediately when it is set, and retrieving it from ViewState
    whenever it is needed, even by the custom control itself. This
    works.

    I am creating the child controls in the PreRender event for the custom
    control. (I have also tried the Init event -- doesn't work, because I
    can't get the PricingModel object from Viewstate because Viewstate
    hasn't been loaded yet.) This works in so as far as the child controls
    get created and their values loaded. However, the values chosen by the
    end user do not persist.

    Here's some simplified code:

    Partial Class PricingCalculator.aspx
    Public Property PricingModel() As PricingModel
    Get
    Return ViewState(Me.ID & ".PricingModel")
    End Get
    Set(ByVal value As PricingModel)
    ViewState(Me.ID & ".PricingModel") = value
    End Set
    End Property

    Private Sub Page_PreRender(sender as object, e as
    System.EventArgs) Handles Me.PreRender
    If PricingModel Is Nothing Then
    Me.lblFinalPrice.Text = "N/A"
    Me.lblWeight.Text = "N/A"
    Else
    CreateControls()
    End If
    End Sub

    Sub CreateControls()
    for each parm as string in PricingModel.ParmNames
    dim ddCtl as DropDownList
    ddCtl = new DropDownList
    ddCtl.ID = "dd_" & parm
    for each val as string in PricingModel.GetParmOptions(parm)
    ddCtl.Items.Add(val)
    next
    AddHandler ddCtl.SelectedIndexChanged, AddressOf
    ChangeHandler
    Me.Controls.Add(ddCtl) ' (actually this is more complex, I
    put it into a table row)
    next
    End Sub

    Sub ChangeHandler(sender as Object, e as EventArgs)
    ' recompute final price ...
    End Sub
    End Class


    So, any suggestions?
     
    Ross Presser, Feb 25, 2008
    #1
    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. Ravikanth[MVP]
    Replies:
    1
    Views:
    1,745
    Christian H
    Jul 24, 2003
  2. sultan zahir khan
    Replies:
    2
    Views:
    447
    Joao S Cardoso [MVP]
    Jul 24, 2003
  3. =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=

    Confusion Over Postback, ViewState and Dynamically Added Controls

    =?Utf-8?B?Q2hyaXN0aWFuIE51bmNpYXRv?=, May 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,347
    Brock Allen
    May 3, 2005
  4. Jeffrey Todd
    Replies:
    1
    Views:
    4,123
    Peter Blum
    Jun 2, 2005
  5. David Hubbard
    Replies:
    2
    Views:
    631
    David Hubbard
    Jan 17, 2006
Loading...

Share This Page