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

Unable to access NewValues of formview

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
allanmark
Asked:
allanmark
  • 3
2 Solutions
 
strickddCommented:
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
 
allanmarkAuthor Commented:
Ok.

I've tried creating the ODS in source (see atatched), but still ahve the same issue - NewValues is null on "ItemUpdating" event.
0
 
allanmarkAuthor Commented:
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
 
allanmarkAuthor Commented:
Found it!!

IUE  -- Idot User Error!

My EditTemplate was using "EVal" instead of "Bind".
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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