471,594 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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
1 1557
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.

Similar topics

8 posts views Thread by Donald Xie | last post: by
3 posts views Thread by Mark Denardo | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

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.