Link to home
Start Free TrialLog in
Avatar of ebuechel
ebuechel

asked on

Trouble with DetailsView, OnItemUpdating

Created a Dynamic DetailsView (basically adding bound fields to DetailsView fields) and I'm having trouble using OnItemUpdating command.

I can populate the form but I'm unable to capture any values on the update.
asp:
 
<asp:DetailsView ID="DetailViewEdit" 
   runat="server"
   AutoGenerateRows="false" 
   OnItemUpdating="DetailViewEdit_ItemUpdating"
   AutoGenerateEditButton="true"
   DefaultMode="Edit" 
   />
 
.cs code:
 
..
  BoundField bf = new BoundField();
  bf.DataField = field.Key;
  bf.HeaderText = field.Name;
  
  DetailViewEdit.Fields.Add(bf);
 
DetailViewEdit.DataKeyNames = field.Key
DetailViewEdit.DataSource = referenceDataTable;
DetailViewEdit.DataBind();
 
...
protected void DetailViewEdit_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
 
e.NewValues  <-- empty
e.OldValues  <-- empty
e.Keys <-- empty

Open in new window

Avatar of surajguptha
surajguptha
Flag of United States of America image

what do you want to do in the item updating event? kinda what you are trying to implement using that event?
Avatar of ebuechel
ebuechel

ASKER

Pulled changed key/value pairs for use in a custom store procedure call.

I could hack the request form - DetailViewEdit.ClientID.Replace('$', '_')  - but that seems a little too sloppy.


        foreach (DictionaryEntry entry in e.NewValues)
        {
              // New Edits, entry.Key : entry.Value

        }
You have to use the DataKey to capture the values.

http://www.mcse.ms/message2536642.html
protected void DetailsView1_ItemUpdating(object sender,
 
DetailsViewUpdateEventArgs e)
 
{
 
int id = (int)DetailsView1.DataKey[0];
 
TextBox txtName = DetailsView1.FindControl("txtName") as TextBox;
 
string newname = txtName.Text;

Open in new window

Not sure if that example exactly relates as I'm creating the Bound Fields on the fly:

...
  BoundField bf = new BoundField();
  bf.DataField = field.Key;
  bf.HeaderText = field.Name;
 
  DetailViewEdit.Fields.Add(bf);

DetailViewEdit.DataSource = referenceDataTable;
DetailViewEdit.DataBind();


DetailViewEdit.FindControl(field.Key) as TextBox;  <-- is null
http://forums.asp.net/p/1034786/1427427.aspx

Found the solution.


thx.
        for (int i = 0; i < DetailViewEdit.Fields.Count; i++)
        {
            DataControlFieldCell cellNormal = detailsView.Rows[i].Cells[0] as DataControlFieldCell;
            detailsView.Fields[i].ExtractValuesFromCell(e.Keys, cellNormal, DataControlRowState.Normal, true);
 
            DataControlFieldCell cellEdit = detailsView.Rows[i].Cells[1] as DataControlFieldCell;
            detailsView.Fields[i].ExtractValuesFromCell(e.NewValues, cellEdit, DataControlRowState.Edit, false);
        }
 
 
        foreach (string key in e.Keys.Keys)
        {
            Utilities.Log.WriteFileEntry("Key(" + key + ") - Value:" + e.Keys[key], Log.Priority.Medium);
        }
 
        foreach (string key in e.NewValues.Keys)
        {
            Utilities.Log.WriteFileEntry("NewValues Key(" + key + ") - Value:" + e.NewValues[key], Log.Priority.Medium);
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial