F
Frank 'Olorin' Rizzi
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
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