ItemUpdating event -- FindControl returns old value of textbox on formview & not new updated value

Eric3141
Eric3141 used Ask the Experts™
on
Note: Answers in C# please - thx!

I have FormView1 with txtbxEditTemplatePopCount TextBox on it.  txtbxEditTemplatePopCount is the name of the textbox on the Edit template -- the same corresponding textbox on the Item & Insert templates has another name.  When the form comes up in Edit mode the current value is in the textbox.  I change the value and click the Update button -- as soon as I do that then on-screen the value in the textbox goes back to what it was originally and the original value is sent as parameter to the method call.  Code is below.  The code is finding the control on the formview but won't take the new value and keeps using the old one.  Very confused!  Help!


    protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
       ....
         TextBox txtbxCensusPopCount  = (TextBox)FormView1.FindControl("txtbxEditTemplatePopCount");

intCensusPopCount = Convert.ToInt32(txtbxCensusPopCount.Text);

strError = BLL_Census(intCensusPopCount );

...

}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Found the answer and posting here in case would help somene later.

Originally in my page_load I had this:

...
            // execute proc to get census data
            myDataTable = DataAccessLayer.uspCensusSelectByFacilityIDdatMonth(intFacilityID, strYearMonth);

            frmvwCensus.DataSource = myDataTable;

            if (myDataTable.Rows.Count == 0)
            {
                frmvwCensus.ChangeMode(FormViewMode.Insert);               }
            else
            {
                frmvwCensus.ChangeMode(FormViewMode.Edit);
                frmvwCensus.DataBind();
            }
...


Problem: when user changed existing data in Edit mode and clicked the UPDATE button then FindControl in _ItemUpdating event saw / used the old value instead of the new value in the textbox.

Solution:  make change below to page_load method and add code below to the _ModeChanging method.

...
if (!IsPostBack)
{
           
// execute proc to get census data
            myDataTable = DataAccessLayer.uspCensusSelectByFacilityIDdatMonth(intFacilityID, strYearMonth);

            frmvwCensus.DataSource = myDataTable;

            if (myDataTable.Rows.Count == 0)
            {
                frmvwCensus.ChangeMode(FormViewMode.Insert);               }
            else
            {
                frmvwCensus.ChangeMode(FormViewMode.Edit);
                frmvwCensus.DataBind();
            }
}...



    protected void frmvwCensus_ModeChanging(object sender, FormViewModeEventArgs e)
    {
        // Note! This code is necessary for FindControl in frmvw_ItemInserting and frmvw_ItemUpdating methods to get the new updated value.
        //       Without the code in this event FindControl will get / use the old values before changes made in the textbox(es) on the
        //       frmvwCensus FormView.
        frmvwCensus.ChangeMode(e.NewMode);

        // execute proc to get census data for given facility for given month
        myDataTable = DataAccessLayer.uspCensusSelectByFacilityIDdatMonth(intFacilityID, strYearMonth);

        frmvwCensus.DataSource = myDataTable;
    }

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial