By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,309 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,309 IT Pros & Developers. It's quick & easy.

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

P: n/a
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)


Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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();

}

"Greg" <na> wrote in message news:OA**************@TK2MSFTNGP10.phx.gbl...
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)

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.