473,320 Members | 1,951 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,320 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 1280
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: Greg | last post by:
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...
0
by: Sinisa Ruzin | last post by:
Hi all, I had problem with dynamically adding/removing controls;ascx, Controls.Add(Page.LoadControl... in the same page of the IBuySpy portal. ASP.NET, C#. I added buttons to the main ASCX loaded...
6
by: Test Message | last post by:
I need to dynamically add an html control to a web form. What I have, thus far, adds the control to the page, but only places it after the closing "</html>" tag. ...
2
by: djk | last post by:
Hi all! Please help me with the following real-trouble: - I have dynamically created controls on page - Everything works unless I set for some controls .Enabled = false In such a case...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
3
by: Mark Denardo | last post by:
I'm trying to dynamically create and add controls to a web page: Label obj1 = new Label(); DropDownList obj2 = new DropDownList(); Controls.Add(obj1); Controls.Add(obj2); But I get the...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.