Professional Web Applications Themes

Maintaining state in programmatically created composite user controls - ASP.NET Building Controls

Hi I'm writing a timesheet application in ASP.NET (framework 1.1, VS 2003) that includes a user control to manage project time allocation (ProjectDetailsControl). This control is a composite control that includes several DropDownList objects for project code, name and so on, as well as TextBox objects for time allocation for each day. I had a problem with this control not rendering when created programmatically, but I've fixed that. Now I have another problem. The control is not maintaining its view state, and I'm not sure how best to implement it. On first entry to the page (and only the first ...

  1. #1

    Default Maintaining state in programmatically created composite user controls

    Hi

    I'm writing a timesheet application in ASP.NET (framework 1.1, VS 2003)
    that includes a user control to manage project time allocation
    (ProjectDetailsControl). This control is a composite control that
    includes several DropDownList objects for project code, name and so on,
    as well as TextBox objects for time allocation for each day. I had a
    problem with this control not rendering when created programmatically,
    but I've fixed that. Now I have another problem.

    The control is not maintaining its view state, and I'm not sure how
    best to implement it. On first entry to the page (and only the first
    entry), the project data is loaded from a database and inserted into
    the page's ViewState. The page then uses this view state information to
    create the appropriate number of ProjectDetailsControl objects.

    I thought I would populate the view state in the page and so, as an
    experiment, I had something along the lines of:

    ViewState["projectDetails_0_ProjectCodeDropDown"] =
    projectReader["projectCode"]

    This is the ID for the project code drop down generated by the
    framework in the HTML. I thought that, since the drop down within my
    ProjectDetailsControl is not picking up its state, I'd look in the
    Page_Load of my ProjectDetailsControl to see what's actually in the
    ViewState object.

    Looking at the ViewState in the ProjectDetailsControl, I discovered
    that it's actually empty, oddly, even though in the page it has lots of
    stuff in it, including all the keys I'd put in myself.

    Bit of a ramble...but bottom line is, how do I maintain state in
    programmatically create composite user controls? I've had a look at
    some articles, such as the Page Controller and Front Controller
    patterns on the MSDN site, but they're not particularly illuminating in
    terms of the actual nuts and bolts of maintaining state.

    Cheers, Mark

    mark.norgate@gmail.com Guest

  2. #2

    Default Re: Maintaining state in programmatically created composite user controls

    Order matters.
    1. Create the control(s).
    2. Add the control(s) to parent's control collection.
    3. Change control's properties - assuming these will be persisted in
    viewstate. Or DataBind.

    If you mix steps 2 and 3, your viewstate will not survive the postback.

    Read the complete explanation by Scott Mitchell.
    [url]http://scottonwriting.net/sowblog/posts/2129.aspx[/url]

    CaffieneRush@gmail.com Guest

  3. #3

    Default Re: Maintaining state in programmatically created composite user controls

    Thanks a lot for the link.

    I've looked at my code, and it is in fact following the order as
    described in the article. I only bind to the database once, when
    !IsPostBack, and the dropdown list is indeed maintaining its list of
    values, and is maintaining the correct selection on each roundtrip.

    The problem is setting the selected item in the dropdown on the first
    entry to the page, when !IsPostBack. What I'm doing is adding all the
    values of the child controls, such as the time allocation for each day,
    and the selected item in the dropdowns, to the ViewState when
    !IsPostBack; then, in my control's Page_Load, I'm trying to retrieve
    these values from the ViewState to set the initial values of the
    various controls when !IsPostBack.

    Perhaps this is a crude way of doing it and violates the Page lifecycle
    pattern. Perhaps I ought instead to set the initial values of the
    controls in the Page and not worry about addressing the ViewState
    directly. But it seems to me that the control itself should be looking
    after the values of its children and the page shouldn't have to worry.
    The only problem with that is that I'd need to know the key of the
    value to put into the ViewState in the page so the controls can
    retrieve it.

    Does that make any sense? Sorry, I'm rambling. What are your thoughts?
    There are a few ways it can be done, but I'm not sure what the best way
    is. I don't really want my composite to start addressing the database
    directly, I'd rather keep that on the page.

    Mark

    mark.norgate@gmail.com Guest

  4. #4

    Default Re: Maintaining state in programmatically created composite user controls

    Let me see if I understand you correctly.

    Within Page_Load check !IsPostBack
    Save values of usercontrol to viewstate
    Followed by
    Retrieve values from viewstate and set various other controls with
    these values.

    I'd check and do one of the following:
    1. As you suggested, set the initial values of the controls directly.
    This has the added benefit of reducing viewstate bloat.

    2. If you're programmatically adding your controls to your page
    (perhaps to a panel or placeholder in the page)
    then make sure you add all the controls to the page before saving and
    retrieving values from viewstate.

    3. Control's vewstate should be tracked automatically at the end of
    your user control's init event.
    The control's lifecycle starts when it is added to the page's control
    tree if it added later like in Page_Load, it will hurry up it's own
    lifecycle to match the page's lifecycle.
    You can ensure that control's viewstate is tracked earlier in your
    control's lifecycle by making a direct call to TrackViewState().
    Previous to this (IsTrackingViewState = False) any modifications and
    additions to the control's viewstate would not be saved.

    CaffieneRush@gmail.com Guest

Similar Threads

  1. Losing Postback state with composite controls
    By Ian in forum ASP.NET Building Controls
    Replies: 0
    Last Post: June 9th, 10:40 PM
  2. Composite Web Control and Dynamically Created Children Controls.
    By Craig in forum ASP.NET Building Controls
    Replies: 9
    Last Post: March 29th, 12:11 AM
  3. Maintaining View State For Dynamically Created Controls
    By Earl Teigrob in forum ASP.NET Building Controls
    Replies: 0
    Last Post: October 7th, 08:09 PM
  4. Replies: 1
    Last Post: November 24th, 09:48 PM
  5. Programmatically created web user control events
    By Jonas in forum ASP.NET Building Controls
    Replies: 1
    Last Post: August 27th, 08:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139