dynamically adding controls, saving, viewing change on page??

G

Greg

In my ASP.NET application I have a button that when pressed, data needs to
be saved based on what the user typed in. I have other controls on the same
page that should be updated as a result of the save, which are all
dynamically created in the code behind and have event handlers registered.

In the page life cycle, event handlers are called after the page is loaded.
The problem is, the save is done after the controls are loaded, so the
change is not reflected because it hasn't made it to the database yet (the
controls are loaded from the database or whatever). And I can't
programatically create the controls from my save because the page has
already loaded, and the event handlers won't be registered.

Example:

I select a customer from the list of controls. I change the customer name
from "Bob" to "Fred". When I hit save, the customers are loaded from the DB
*before* the save event is called, so "Bob" still displays even though it
was changed to "Fred".

One work around is to put a "response.redirect" to the page in the save
event, but this creates two postbacks which seems totally inefficient.

Logically, I need to move the save event to fire before the controls are
loaded, but I don't know if/how this is possible. Again, the save event
handler depends on a control on the page that is posting back.

Psuedo code:

Page_Load <-- gets fired before save
LoadCustomerNameControls
cmdSave += new CommandEventHandler(SaveCustomerName)

SaveCustomerName (object sender, CommandEventArgs e)
RunSql("update customername set name=" + SomeField.Text)
 
G

Greg

Ok, I fugred this out...

First of all, it *is* possible to load controls and event handlers after you
fire an event (sorry, I misspoke earlier, it's been a long day). Due to
some other complexities of the UI I was working with, I basically had to add
some better checking on when to load up the controls. I ended up needing to
determine which control was causing the postback, firing off the save event,
and then loading the other controls programatically as normal. Example:

if (Request.Form["__EVENTTARGET"] != "lbSave")

{

LoadCustomers();

}
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top