?
Solved

Unable to access NewValues of formview

Posted on 2009-04-28
4
Medium Priority
?
692 Views
Last Modified: 2013-12-17
Greetings all

I am trying to access the "NewValues" of a formView Control.

I started out with a DataTable as a Datasource (all done in coide behind) and subsequently read that the NewValues are only available with controls like the SqlDataSource and ObjectDataSource.

I have subsequently converetd my code (attached) to use an ObjectDataSource (all done in code behind) that is bound to a DataTable (hope I've said that correclty!).

When I try to access the NewValues (or OldValues in the "FvSpousal_ItemUpdating" event, they are still null.

What am I misisng?


In advance, thanks!!!

    allanmark
CODE BEHIND- BUSINESS LAYER:
 
public static DataTable GetCouple(int coupleId)
        {
            DataTable dtOurCouple = new DataTable();
 
            dtOurCouple = DataAccess.ReturnCouple(coupleId);
 
            return dtOurCouple;
        }
 
 
CODE BEHIND -  THE PAGE:
 
public partial class People : System.Web.UI.Page
{
    private ObjectDataSource ods;
    ...............    
 
    private bool coupleIsFound;
 
 
    protected void Page_Load(object sender, EventArgs e)
    {
        ods = new ObjectDataSource();
 
        if (Page.IsPostBack)
        {
            if (Session["portfolios"] != null)
            {
                dtThePortfolios = (DataTable)Session["portfolios"];
            }
            else
            {
                GetPortfolios();
            }
 
            if (Session["titles"] != null)
            {
                dtTheTitles = (DataTable)Session["titles"];
            }
            else
            {
                GetTitles();
            }
 
            if (Session["couples"] != null)
            {
                Session["selectedCouple"] = lbxCouples.SelectedIndex;
                couplesList = (List<CoupleName>)Session["couples"];
                lbxCouples.DataSource = couplesList;
                lbxCouples.DataTextField = "Name";
                lbxCouples.DataValueField = "Id";
                lbxCouples.DataBind();
                lbxCouples.SelectedIndex = (int)Session["selectedCouple"];
 
                //if (Session["theCouple"] != null)
                if (Session["ods"] != null)
                {
                    ods = (ObjectDataSource)Session["ods"];
                    //theCouple = (DataTable)Session["theCouple"];
                }
                else
                {
                    GetCoupleDetails(couplesList[lbxCouples.SelectedIndex].Id);
                }
            }
            else
            {
                GetCouples();
            }
        }
        else
        {
            // Not PostBack.
            GetPortfolios();
            GetTitles();
 
            GetCouples();                        
            Session["selectedCouple"] = lbxCouples.SelectedIndex;
           
        }
    }
 
    protected void fvSpousal_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        switch (e.CommandName.ToUpper())
        {
            case "EDIT":
                pnlSelector.Enabled = false;
                fvSpousal.ChangeMode(FormViewMode.Edit);
                BindSpousalFV();
 
                break;
            case "INSERT":
                pnlSelector.Enabled = false;
                fvSpousal.ChangeMode(FormViewMode.Insert);
                BindSpousalFV();
                break;
            case "UPDATE":
 
                break;
            case "DELETE":
 
                break;
            case "CANCEL":
 
                break;
            default:
                break;
        }
    }
 
    protected void FvSpousal_ItemUpdating(Object sender, FormViewUpdateEventArgs e)
    {
        // Todo: No validating (fields present, dates, etc will be done for the prototype.
        //       This will be implemented in Phase 2.
        if (fvSpousal.CurrentMode == FormViewMode.Edit)
        {
            foreach (DictionaryEntry de in e.NewValues)
            {
                theKey = de.Key.ToString();
                theValue = de.Value.ToString();
            }
        }
        else
        {
            // Insert new record.
        }
        
    }
 
    protected void GetCouples()
    {
        couplesList = BusinessLogic.Business.GetCoupleNames(chkActive.Checked, chkInvolved.Checked);
	....................
    }
 
    protected void GetCoupleDetails(int coupleId)
    {        
          ods.ID = "odsCouple";
          ods.SelectMethod = "GetCouple";
          ods.TypeName = "BusinessLogic.Business";
          ods.Selected += new ObjectDataSourceStatusEventHandler(ods_Selected);
 
          if (ods.SelectParameters.Count == 0)
          {
              Parameter workParam = new Parameter("coupleId", DbType.Int32);
              workParam.Direction = ParameterDirection.Input;
              workParam.DefaultValue = lbxCouples.SelectedValue;
              ods.SelectParameters.Add(workParam);
          }
          else
          {
              ods.SelectParameters[0].DefaultValue = lbxCouples.SelectedValue;
          }
 
          ods.Select();
          Session["ods"] = ods;
 
        BindSpousalFV();
 
        //if (theCouple != null)
        if (coupleIsFound)
        {
            Button btnEd = fvSpousal.FindControl("btnEditI") as Button;
            btnEd.Enabled = true;
            Button btnDel = fvSpousal.FindControl("btnDeleteI") as Button;
            btnDel.Enabled = true;
        }
        else
        {
            ScriptManager.RegisterStartupScript(Page, this.GetType(), "id", "alert('Couple record not found. Please contact the Sytem Administrator.');", true);
            lbxCouples.SelectedIndex = 0;
        }
        SetUpdatePanels();
 
    }
 
    protected void GetPortfolios()
    {        
        dtThePortfolios = BusinessLogic.Business.GetPortfolios();
        Session["portfolios"] = dtThePortfolios;        
    }
 
    protected void GetTitles()
    {
        dtTheTitles = BusinessLogic.Business.GetTitles();
        Session["titles"] = dtTheTitles;
    }
 
    protected void chkActive_CheckedChanged(object sender, EventArgs e)
    {
        GetCouples();
        uppnlSelector.Update();
    }
    protected void chkInvolved_CheckedChanged(object sender, EventArgs e)
    {
        GetCouples();
        uppnlSelector.Update();
    }
 
    protected void fvSpousal_DataBound(object sender, EventArgs e)
    {
        // Todo:  With all this repetition, is there not a generic method that we can develop and
        //        just pass it parameters?
 
        // For the ItemTemplate:
 
        DataRowView drv = (DataRowView)fvSpousal.DataItem;
        
        switch (fvSpousal.CurrentMode)
        {
            case FormViewMode.ReadOnly:
                // Portfolio dropdown for couple
                DropDownList ddlPortF_CI = fvSpousal.FindControl("ddlPortfolio") as DropDownList;
 
                // Title dropdown for Spouse2
                DropDownList ddlTitleSP2_CN = fvSpousal.FindControl("ddlTitleSP2_CN") as DropDownList;
		......................................
                break;
        } // switch (fvSpousal.CurrentMode)
 
 
    }
 
    protected void lbxCouples_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetCoupleDetails(couplesList[lbxCouples.SelectedIndex].Id);
        Session["selectedCouple"] = lbxCouples.SelectedIndex;
    }
 
        protected void btnInsertC_Click(object sender, EventArgs e)
    {
        pnlSelector.Enabled = false;
        fvSpousal.ChangeMode(FormViewMode.Edit);
        BindSpousalFV();
 
    }
 
    protected void ods_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        string ggg = e.ReturnValue.GetType().ToString();
        if (e.Exception == null)
        {
            DataTable dt = (DataTable)e.ReturnValue;
            if (dt != null)
            {
                coupleIsFound = true;
            }
            else
            {
                coupleIsFound = false;
            }
        }
        else
        {
            coupleIsFound = false;
        }   
    }
 
    protected void BindSpousalFV()
    {
        string[] keys = { "C_Id" };
        //fvSpousal.DataSource = theCouple;   /// A dataTable
 
        fvSpousal.DataSource = ods;
        fvSpousal.DataKeyNames = keys;
        fvSpousal.DataBind();
    }
 
    protected void SetUpdatePanels()
    {
        uppnlSpousal.Update();
    }
  
}

Open in new window

0
Comment
Question by:allanmark
  • 3
4 Comments
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 400 total points
ID: 24251332
Using datasources in the code-behind can be tricky. It may be easier if you can create the ObjectDataSource and bind it to a method in the code-behind that returns the data table.

If you cannot do this, make sure you are binding the datasource to your object on each postback. Also make sure you are creating the datasource the same way on each postback.
0
 

Author Comment

by:allanmark
ID: 24253611
Ok.

I've tried creating the ODS in source (see atatched), but still ahve the same issue - NewValues is null on "ItemUpdating" event.
0
 

Author Comment

by:allanmark
ID: 24254040
Not sure what this means ...

I re-checked the "FormViewUpdateEventArgs", in the fvSpousal_ItemUpdating event and the "Keys" item now contains the correct key data (oldValues and NewValues remain null).
0
 

Accepted Solution

by:
allanmark earned 0 total points
ID: 24254595
Found it!!

IUE  -- Idot User Error!

My EditTemplate was using "EVal" instead of "Bind".
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question