Page_Init and Control Events

1

1388-2/HB

I learned that if you want asp.net to maintain viewstate of a dynamic
control, you instantiate / add it to the html form during the page_init
phase. At least that's the way I've been coding my pages for the past few
years.

But what if the code within a control event deems it necessary to change the
dynamic controls of the page? For example, imagine a table where each row
exposes a database record for editing. The row has many cells, checkboxes,
textboxes, etc. and ibviously it will be full of dynamic controls. Let's
also give each row a dynamic "delete" button if they are logged in as admin,
so administrators can kill records. This should be easy, because removing
dynamic controls from the table obviates the need to maintain viewstate or
worry about them any longer.

However, what if we also give users a "new record" button? If the user
clicks the "new record" this performs a postback; and first the UI is
generated in page_init, and then the "new record" button event fires. Here
is where the record is created. And now the table we make in page_init is
missing this record, because it did not exist yet during page_init.

By the time the event fires, I assume it is too late to add the appropriate
controls to the html form for our new record, and still have the controls in
that row maintain viewstate.

Currently I solve a problem like this by creating the new record in the
button event, and then simply redirecting to myself (basically I throw away
the current page processing, do not render it to the client, and begin page
processing all over again using the latest database information). But I
really don't like doing it this way at all. Are my assumptions correct and
are there any best practices / alternatives to this?
 
B

bruce barker

you can create dynamic controls after oninit. what is import is that on
the next postback, the onint routine must regenerate the dynamic control
that where added on the previous render.

with this model, you can delete and add controls in a button click
event, etc. you just need to save state for the init regenerate.

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top