Page Load event, Request Form, and controls

Discussion in 'ASP .Net' started by Frank 'Olorin' Rizzi, Jan 9, 2004.

  1. Hello everyone.
    This is quite convoluted, but I'll try to make it simple.

    I have a couple of bottom-line questions (I guess):

    1~ what happens between the Page_Load routine in the code behind of an aspx
    page and the
    presentation of the page to the user?

    2~ is it true that the engine takes data from the Request.Form and puts them
    in the
    page's controls automatically?

    Now some more information as to why I have these questions :)

    I am working on an aspx page that works like this:

    On the first load, go to a data repository, build an object with data from
    it;
    the object has a variable number of parameters (0..n), each with a name and
    a value;
    for each of these parameters, create on the page a text box containing the
    parameter's value;
    (the parameter's name is displayed as simple text right before the text
    box);
    display page to user

    the user can change the parameter's values, and then click "Calc";
    at this point the page needs to perform some business logic on the various
    parameters,
    based on the values entered by the user (remember the object that was built
    on the first
    load with all the parameters? I save it to view state, and pass it now the
    values the user entered...
    the object knows how to apply all required changes);
    now rebuild the page, displaying the new values of the parameters.

    So, what I am doing is I am saving the object in the view state,
    and I build the text boxes with a known prefix (say
    "dyncontrols_<parameter_name>");
    on the second (and subsequent) loads, I gather the data as entered by the
    user
    by looking for elements in the Request.Form that start with "dyncontrols_".
    I get the object from ViewState, and pass it these data.
    The object appropriately recalculates all that is needed.
    So, for ex: consider that our object has (on the first load):
    parameter_A = 2
    parameter_B = 3
    parameter_C = parameter_A + parameter_B => 5
    the user changes parameter_A to 4, and clicks "Calculate"
    we get
    dyncontrol_parameter_A = 4
    dyncontrol_parameter_B = 3
    dyncontrol_parameter_C = 5
    from Request.Form
    we get the object (with the old values 3, 2, and 5 from ViewState) and tell
    it
    to update. Fine, the object (when I'm stepping through in debug mode) now
    says
    parameter_A = 4
    parameter_B = 3
    parameter_C = 7
    so, we rebuild the page as on the first load, using these values...
    dyncontrol_parameter_A = 4
    dyncontrol_parameter_B = 3
    dyncontrol_parameter_C = 7
    when debugging, I see that this indeed happens right.
    Then the Page_Load routine is over.
    However, the web page sent to the client shows the following:
    parameter_A = 4
    parameter_B = 3
    parameter_C = 5 (the old value)

    Apparently, *something* happens between the end of the Page_Load routine and
    the displaying of the page,
    and I can only guess that the engine, at that point, finds the following
    entry:
    dyncontrol_parameter_C => 5
    in the Request.Form object, and uses that value for the control with that
    name (overwriting the 7 I put there).

    I've tried to delete the entries from the Request.Form object as I gather
    the user input, to avoid
    this, but, of course, Request.Form is read-only.

    I got around the problem by using a different prefix for the dynamic
    controls every time (dyncontrols_1_<parameter_name>
    on the first load, dyncontrols_2_<parameter_name> on the second load and so
    on).
    This works, making me really think that the engine uses the name of the
    entries in the Request.Form object
    to populate any control with that name on the page.

    yet, it really feels like a big hack,
    and I'd like to have a better solution.

    So, if anyone has a better solution, or can suggest what to so, I'd be glad
    to hear it.

    For ex: if we know that this linking of values in Request.Form and values in
    the controls on the page
    takes place in the standard handler for some event between the page load and
    the page delivery,
    I could intercept that event and overwrite it so that the values are no
    overwritten...

    i can only think that the Render event is fired after the page load event
    (or is there a PreRender event as well? ).

    Thanks in advance for any suggestion,
    Frank "Olorin" Rizzi
    Frank 'Olorin' Rizzi, Jan 9, 2004
    #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. Brian Birtle
    Replies:
    2
    Views:
    2,017
    John Saunders
    Oct 16, 2003
  2. venky

    Page Load and webcontrol page load

    venky, Jul 6, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    3,098
    venky
    Jul 11, 2005
  3. GauravGupta
    Replies:
    4
    Views:
    2,101
    Shilpa1234
    Jan 19, 2012
  4. Matcon
    Replies:
    3
    Views:
    21,572
    Matcon
    May 28, 2008
  5. matthias s
    Replies:
    0
    Views:
    163
    matthias s
    Sep 20, 2007
Loading...

Share This Page