Retrieving values from dynamic controls without reloading the control tree

momeInverse

New Member
Question:Is there any way to retrieve the values from dynamic controls without reloading the entire control tree?How the application currently works:The application is a series of forms that need to be filled out and saved. We are using one "container" page that is basically responsible for navigating between the forms, as well as determining which form to show. Once a form is completed, the values are saved and we post back to the container page again which will then determine which form (control) to display next. This is repeated until all forms are filled out.What the problem is:The container is responsible for determining which control to render, so when the Page_Init method is called, it will determine what control to load and add it to a placeholder in the container. However, since we are dynamically adding the controls, when a postback occurs, the init method is called which reloads the controls. In some scenarios this is desired if we truely need to post back to the page and display the same form back to the user (custom server side validation for example). However, in the scenario where the user has completed the form, we are reloading all the controls in order to save them, and then redirect user to the next form which is causing performance problems.Code:I'm not sure if code is required for this question, but the following is basically a stripped down version of the application. If you have any questions I would be happy to answer. Thanks.Container.aspx:\[code\]<asp:PlaceHolder ID="m_customControlPlaceHolder" runat="server" ></asp:PlaceHolder><asp:Button ID="m_buttonSaveContinue" runat="server" OnClick="saveContinue_Click" />\[/code\]Container.aspx.cs:\[code\]protected void Page_Init(object sender, EventArgs e){ loadControls();}private void loadControls(){ m_customControlPlaceHolder.Controls.Clear(); var control = Page.LoadControl("SomeControl.ascx"); m_customControlPlaceHolder.Controls.Add(control);}protected void saveContinue_Click(object sender,EventArgs e){ saveControls(page.Controls); // Redirect back to the same page, which will now render a different user control.}private void saveControls(ControlCollection controls){ // Loop through all controls on the page. foreach (Control control in controls) { // Is the control a data source? var fieldDataSource = control as FieldDataSource; if (fieldDataSource != null { // Find the control the data source is associated with. var controlToBind = FindControl(fieldDataSource.ControlToBindTo); if (controlToBind != null) { // Take the value in controlToBind, and save it to fieldDataSource.DataSourceID } } // If the control has children, recursively call this method. else if (control.HasControls()) { saveControls(control.Controls); } }}\[/code\]SomeControl.ascx:\[code\]<asp:TextBox ID="m_textBoxToSave" runat="server"></asp:TextBox><custom:FieldDataSource runat="server" ID="m_textBoxToSaveFieldDataSource" ControlToBindTo="m_textBoxToSave" DataSourceID="{Some Database ID}" />\[/code\]
 
Back
Top