473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamic runtime controls, postbacks, Page_Init, Viewstate et al

Hi,

I've searched the newsgroup and other sources to understand how to handle
runtime controls and see I'm not the only one who's confused, but I'm still
not quite sure of the best way to handle from all the various
explanations/answers. I'm attempting the typical scenario...

I create a variable number of controls at runtime based on parameters from a
database.

I understand that I need to re-initialize the controls with the same ID upon
post-back so that the code-behind can see the controls and it's properties.
I also learned that I need to do so in Page_Init instead of Page_Load or the
code-behind won't be able to see the value of the control as
entered/selected by the user. (What's really odd to me when I tried to
initialize in Page_Load, is that somehow, somewhere, the value of those
controls must still exist because, even though I can't get to the value in
code-behind, the value does still get passed back to the browser in the
returning page. I just don't get why I can't get to that value if it's still
there.)

So anyway, I figure I should avoid going back to the database on postback.
So I store the initialization parameters in Viewstate. But of course,
Viewstate isn't available yet in Page_Init. So it seems that I'm forced to
go back to the database to get the initialization parameters (or use Session
or other memory state which I don't think I want to do). But this seems to
defeat almost the whole purpose of Viewstate enabled controls in the
firstplace. If I have to go back to the database anyway, what is the value
in a viewstate enabled runtime control?

Is returning to the database the only/best way to handle these controls, or
am I missing a trick?

Thanks in advance,
Chuck
Nov 18 '05 #1
4 6701
Hopefully, the following article from the .Net SDK should answer all of your
questions. Note that All classes which inherit System.Web.UI.Control
(including Page) follow this same sequence of events. Putting the right code
in the right event handler is the key.

http://msdn.microsoft.com/library/de...nLifecycle.asp
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chuck Ritzke" <CHUCK AT MYACTUARY DOT COM> wrote in message
news:eS*************@TK2MSFTNGP12.phx.gbl...
Hi,

I've searched the newsgroup and other sources to understand how to handle
runtime controls and see I'm not the only one who's confused, but I'm still not quite sure of the best way to handle from all the various
explanations/answers. I'm attempting the typical scenario...

I create a variable number of controls at runtime based on parameters from a database.

I understand that I need to re-initialize the controls with the same ID upon post-back so that the code-behind can see the controls and it's properties. I also learned that I need to do so in Page_Init instead of Page_Load or the code-behind won't be able to see the value of the control as
entered/selected by the user. (What's really odd to me when I tried to
initialize in Page_Load, is that somehow, somewhere, the value of those
controls must still exist because, even though I can't get to the value in
code-behind, the value does still get passed back to the browser in the
returning page. I just don't get why I can't get to that value if it's still there.)

So anyway, I figure I should avoid going back to the database on postback.
So I store the initialization parameters in Viewstate. But of course,
Viewstate isn't available yet in Page_Init. So it seems that I'm forced to
go back to the database to get the initialization parameters (or use Session or other memory state which I don't think I want to do). But this seems to
defeat almost the whole purpose of Viewstate enabled controls in the
firstplace. If I have to go back to the database anyway, what is the value
in a viewstate enabled runtime control?

Is returning to the database the only/best way to handle these controls, or am I missing a trick?

Thanks in advance,
Chuck

Nov 18 '05 #2
Hi again,

Thanks for the link, but I'm not sure whether it answers my question, unless
the answer is... "you are stuck getting control initialization parameters
from the database on each trip."

The article seems to say...

(1) ViewState and ProcessPostBackData run in between Init and Load events.
(2) There are no LoadViewState or ProcessPostBackData events for me to write
code in so I can't initialize my controls after LoadViewState has occurred,
but before ProcessPostBackData has occurred.
(3) I can't use ViewState during Init event to get my parameters because
ViewState hasn't been loaded.
(4) And I can't wait to re-initialize my controls in Load event because
ProcessPostBackData has already occurred and so I'll not get my posted
values.

Conclusion: I need to find another way to get my control initialization
parameters when the page posts back.

Is that the conclusion I should draw? Or can I somehow force LoadViewState
to happen before I leave Init event?

Thanks again,
Chuck

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:un**************@tk2msftngp13.phx.gbl...
Hopefully, the following article from the .Net SDK should answer all of your questions. Note that All classes which inherit System.Web.UI.Control
(including Page) follow this same sequence of events. Putting the right code in the right event handler is the key.

http://msdn.microsoft.com/library/de...nLifecycle.asp --
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chuck Ritzke" <CHUCK AT MYACTUARY DOT COM> wrote in message
news:eS*************@TK2MSFTNGP12.phx.gbl...
Hi,

I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm still
not quite sure of the best way to handle from all the various
explanations/answers. I'm attempting the typical scenario...

I create a variable number of controls at runtime based on parameters from a
database.

I understand that I need to re-initialize the controls with the same ID

upon
post-back so that the code-behind can see the controls and it's

properties.
I also learned that I need to do so in Page_Init instead of Page_Load or

the
code-behind won't be able to see the value of the control as
entered/selected by the user. (What's really odd to me when I tried to
initialize in Page_Load, is that somehow, somewhere, the value of those
controls must still exist because, even though I can't get to the value

in code-behind, the value does still get passed back to the browser in the
returning page. I just don't get why I can't get to that value if it's

still
there.)

So anyway, I figure I should avoid going back to the database on postback. So I store the initialization parameters in Viewstate. But of course,
Viewstate isn't available yet in Page_Init. So it seems that I'm forced to go back to the database to get the initialization parameters (or use

Session
or other memory state which I don't think I want to do). But this seems to defeat almost the whole purpose of Viewstate enabled controls in the
firstplace. If I have to go back to the database anyway, what is the value in a viewstate enabled runtime control?

Is returning to the database the only/best way to handle these controls,

or
am I missing a trick?

Thanks in advance,
Chuck


Nov 18 '05 #3
You can override ANY of those events. Just because the ASPX template that
comes bundled with Visual Studio.Net doesn't have them pre-written for you
doesn't mean they aren't there, or that you can't use them. All you have to
do is use what's available (wisely), and put your code in the proper
sequence.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chuck Ritzke" <CHUCK AT MYACTUARY DOT COM> wrote in message
news:uM*************@TK2MSFTNGP09.phx.gbl...
Hi again,

Thanks for the link, but I'm not sure whether it answers my question, unless the answer is... "you are stuck getting control initialization parameters
from the database on each trip."

The article seems to say...

(1) ViewState and ProcessPostBackData run in between Init and Load events.
(2) There are no LoadViewState or ProcessPostBackData events for me to write code in so I can't initialize my controls after LoadViewState has occurred, but before ProcessPostBackData has occurred.
(3) I can't use ViewState during Init event to get my parameters because
ViewState hasn't been loaded.
(4) And I can't wait to re-initialize my controls in Load event because
ProcessPostBackData has already occurred and so I'll not get my posted
values.

Conclusion: I need to find another way to get my control initialization
parameters when the page posts back.

Is that the conclusion I should draw? Or can I somehow force LoadViewState
to happen before I leave Init event?

Thanks again,
Chuck

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:un**************@tk2msftngp13.phx.gbl...
Hopefully, the following article from the .Net SDK should answer all of your
questions. Note that All classes which inherit System.Web.UI.Control
(including Page) follow this same sequence of events. Putting the right

code
in the right event handler is the key.

http://msdn.microsoft.com/library/de...nLifecycle.asp
--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chuck Ritzke" <CHUCK AT MYACTUARY DOT COM> wrote in message
news:eS*************@TK2MSFTNGP12.phx.gbl...
Hi,

I've searched the newsgroup and other sources to understand how to handle runtime controls and see I'm not the only one who's confused, but I'm

still
not quite sure of the best way to handle from all the various
explanations/answers. I'm attempting the typical scenario...

I create a variable number of controls at runtime based on parameters from
a
database.

I understand that I need to re-initialize the controls with the same ID
upon
post-back so that the code-behind can see the controls and it's

properties.
I also learned that I need to do so in Page_Init instead of Page_Load
or the
code-behind won't be able to see the value of the control as
entered/selected by the user. (What's really odd to me when I tried to
initialize in Page_Load, is that somehow, somewhere, the value of
those controls must still exist because, even though I can't get to the value in code-behind, the value does still get passed back to the browser in
the returning page. I just don't get why I can't get to that value if it's still
there.)

So anyway, I figure I should avoid going back to the database on

postback. So I store the initialization parameters in Viewstate. But of course,
Viewstate isn't available yet in Page_Init. So it seems that I'm
forced to go back to the database to get the initialization parameters (or use Session
or other memory state which I don't think I want to do). But this
seems to defeat almost the whole purpose of Viewstate enabled controls in the
firstplace. If I have to go back to the database anyway, what is the value in a viewstate enabled runtime control?

Is returning to the database the only/best way to handle these

controls, or
am I missing a trick?

Thanks in advance,
Chuck



Nov 18 '05 #4
Thanks - I had to learn about overriding before I could figure out what you
were getting at, but I finally got it to work. Just in case there is anybody
else out their struggling with the same problem, here is what appears to be
working for me. I added the following routine to the page which allows me to
get at ViewState and initialize my runtime controls before the posted data
is loaded into the controls...

Protected Overrides Sub LoadViewState(ByVal savedState As Object)

Dim dtControlData As System.Data.DataTable

MyBase.LoadViewState(savedState)

'dtControlData contains my runtime control parameters, created and saved
in ViewState the previous time around
dtControlData = ViewState("dtControlData")

'AddSurveyControls re-adds all my runtime controls
AddSurveyControls(dtControlData)

End Sub

Then if I need any new controls that are dependent upon previously posted
entries, I add those in Page_Load or later and add them to my dtControlData
table for the next postback.

Thanks again,
Chuck
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:uf**************@TK2MSFTNGP12.phx.gbl...
You can override ANY of those events. Just because the ASPX template that
comes bundled with Visual Studio.Net doesn't have them pre-written for you
doesn't mean they aren't there, or that you can't use them. All you have to do is use what's available (wisely), and put your code in the proper
sequence.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chuck Ritzke" <CHUCK AT MYACTUARY DOT COM> wrote in message
news:uM*************@TK2MSFTNGP09.phx.gbl...
Hi again,

Thanks for the link, but I'm not sure whether it answers my question, unless
the answer is... "you are stuck getting control initialization parameters
from the database on each trip."

The article seems to say...

(1) ViewState and ProcessPostBackData run in between Init and Load events. (2) There are no LoadViewState or ProcessPostBackData events for me to

write
code in so I can't initialize my controls after LoadViewState has

occurred,
but before ProcessPostBackData has occurred.
(3) I can't use ViewState during Init event to get my parameters because
ViewState hasn't been loaded.
(4) And I can't wait to re-initialize my controls in Load event because
ProcessPostBackData has already occurred and so I'll not get my posted
values.

Conclusion: I need to find another way to get my control initialization
parameters when the page posts back.

Is that the conclusion I should draw? Or can I somehow force LoadViewState to happen before I leave Init event?

Thanks again,
Chuck

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:un**************@tk2msftngp13.phx.gbl...
Hopefully, the following article from the .Net SDK should answer all of
your
questions. Note that All classes which inherit System.Web.UI.Control
(including Page) follow this same sequence of events. Putting the
right code
in the right event handler is the key.

http://msdn.microsoft.com/library/de...nLifecycle.asp --
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Chuck Ritzke" <CHUCK AT MYACTUARY DOT COM> wrote in message
news:eS*************@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I've searched the newsgroup and other sources to understand how to

handle
> runtime controls and see I'm not the only one who's confused, but I'm still
> not quite sure of the best way to handle from all the various
> explanations/answers. I'm attempting the typical scenario...
>
> I create a variable number of controls at runtime based on parameters
from
a
> database.
>
> I understand that I need to re-initialize the controls with the same

ID upon
> post-back so that the code-behind can see the controls and it's
properties.
> I also learned that I need to do so in Page_Init instead of
Page_Load or the
> code-behind won't be able to see the value of the control as
> entered/selected by the user. (What's really odd to me when I tried
to > initialize in Page_Load, is that somehow, somewhere, the value of

those > controls must still exist because, even though I can't get to the value
in
> code-behind, the value does still get passed back to the browser in

the > returning page. I just don't get why I can't get to that value if it's still
> there.)
>
> So anyway, I figure I should avoid going back to the database on

postback.
> So I store the initialization parameters in Viewstate. But of course, > Viewstate isn't available yet in Page_Init. So it seems that I'm forced
to
> go back to the database to get the initialization parameters (or use
Session
> or other memory state which I don't think I want to do). But this

seems
to
> defeat almost the whole purpose of Viewstate enabled controls in the
> firstplace. If I have to go back to the database anyway, what is the

value
> in a viewstate enabled runtime control?
>
> Is returning to the database the only/best way to handle these

controls, or
> am I missing a trick?
>
> Thanks in advance,
> Chuck
>
>



Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: wASP | last post by:
Hello again to all of you geniuses, I'm having a problem trying to load dynamic controls at the initialization phase. I've read the docs, and I thought I had it figured out:...
6
by: Mark | last post by:
I have been working for quite some time on this issue which in theory should be quite simple. The problem is that the Cancel and Save events are not fired when their respective buttons are...
4
by: Tom Wisnowski | last post by:
Hello all, Here is a simple example of what I'm doing... Create a ASPX page Add a PlaceHolder(plch) and a Button(btn) to the page. In the code behind on Page_load add the following......
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
3
by: Stu | last post by:
Hi, I am creating a control in a PlaceHolder like so: Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click Dim ctrl As...
1
by: Andrew Robinson | last post by:
I have a <asp:table> control with a large number of dynamically created LinkButtons. I am using the command event, command name and command argument values in my LinkButtons to trigger actions...
2
by: BillE | last post by:
I have a web form which has dynamic controls for order entry, (item selected, #,etc.) which are wired to an event handler which fires when the dynamic controls content changes. These controls...
4
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox...
5
by: Hans Kesting | last post by:
Hi, Is there good information about the asp.net page lifecycle in combination with dynamically loaded controls? Or on "how to build dynamic controls"? I keep hitting problems where values are...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.