• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 633
  • Last Modified:

When I finished adding a new "item" on my FormView controlled ASP.NET web page, on PostBack in the DataBound event my FormView has no instantiated controls, why?

In my DataBound event handler for my FormView I try to access a couple of controls via the FindControl method. When I go into this page with an "item" selected, whereby populating the page, and then do an update..., on postback, switching to ReadOnly mode my controls are found.

But, if I go into the form first in InsertMode and then "save" my item, on postback, during the DataBound event handler the fvMyForm.Controls.Count = 0.  Why is that?
0
Bruce
Asked:
Bruce
  • 3
  • 3
2 Solutions
 
williamcampbellCommented:
Some Clarification might help us...
When I go into this page with an "item" selected:   Please Define Item, what's on the page
whereby populating the page: Populating what with what?
and then do an update: How? Button?
switching to ReadOnly mode my controls are found: How do you Switch? Define  Readonly Mode.
InsertMode and then "save" my item: Define InsertMode. Are you Saving to a Database, what does save mean?

Thanks

wc
0
 
BruceAuthor Commented:
Sorry, I know this is complicated to explain.  It took awhile to formulate so that I didn't include lots of impertinent information.

I'll start again...here's the general workflow.  

The user, from another page, can select an item to "View" or select "New".  If they select View I go to the page in questions, with the FormView control on it, and start in ReadOnly mode.  Inside the FormView databound event handler I switch on the FormMode and make visible some controls and make others invisible.

When the user selects "New" from the other page I redirect to the page in question, with the FormView, and set the mode to "Insert".  The insert template has all the empty controls.  Data is entered and they click the save button.  During the postback,  it goes through the page_load, etc, then hits my button handler event which walks through all the controls and takes the data and does an insert into a SQL database, then calls ChangeMode,  then it hits the breakpoint I have set in the DataBound event handler for the FormView.  Its there where I am trying to access controls and the formview has 0.  FYI, it did not hit the breakpoint in the FormView OnDataBound eventhandler before it hit my Save code (where all the controls where available to have data extracted from them).

Sorry, this is complicated.  

Thank you for any help!
0
 
williamcampbellCommented:
Do you want to clear the controls after the save?
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
BruceAuthor Commented:
No, I am trying to just move to ReadOnly FormViewMode after the insert of the data.  What's odd is that I use the same path of code when I am doing an Update.  During Update the controls are poulated form the database, changes are made by the user, then the user clicks "Save" and the same code is hit, essentially.

Note: This is unrelated to the what I think is a page life cycle issue but for posterity the save code uses a data adapter and I setup Insert, Update and Delete commands and populate a DataSet and had it to that code.  So I call the same code, essentially, whether I am Inserting a new widget record or updating an existing widget record.

The odd thing is I don't have the same issue when I am doing an update in my work flow.  In the update wf I set the form to be in Edit FormViewMode, the user interacts with the screen, then clicks save and the code executes without issue (saves the data and then changes to readonly mode).

Sincerely appreciate you sticking with me on this williamcampbell!
protected void btnSave_Click(object sender, EventArgs e)
    {

        try
        {
            if (Page.IsValid)
            {
                Save();
                Common.RemoveSession();
                GetExtract(hidExtractID.Value);
                fvExtractDetail.ChangeMode(FormViewMode.ReadOnly);
                this.CheckForDataChanges = false;
                ((Main)Page.Master).SetMessage("Data Saved Successfully");
            }


        }
        catch (Exception ex)
        {
            ((Main)Page.Master).SetMessage(ex.Message);
        }

    }

Open in new window

0
 
BruceAuthor Commented:
OK, it wasn't a page life cycle issue.  It was a data issue.  On postback after the data was persisted to the database, and the mode changed to "ReadOnly", the select stored proc to retrieve the data just entered was returning an empty resultset for the ObjectDataSource that was connected to the FormView.

Why a lack of data for the FormView causes it to have no controls is confusing to me.    Any ideas?

If not, I am happy to award points for effort.
0
 
williamcampbellCommented:
Did an exception occur that prevented the page from fully rendering? Turn on exceptions under the Debug menu.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now