Inputs inside ListView doesn't change values from old to recently set on ItemUpdating event
- by Tema
Hi,
I would appreciate if someone help me to understand this situation.
I do not know why but when i edit selected ListView item (containing few TextBoxes) and then press Update button in the ItemUpdating event i always get old values instead of those which were typed recently.
Why?
I do not use Page_Load event so i do not need check on PostBack
I try to get value before i bind data from DB to ListView, so it can't override recently typed values
I tried to get TextBoxes values in different Event handlers - ItemCommand, ItemUpdating, ItemDataBound - result si always the same
Collection NewValues and OldValues are always empty (i think this is because i don't use SqlDataSource control)
The only one way i can get new values - is to check Request, but in this case i can't use control validators ... so probably it is bad idea to work with only request.
This is the code of ItemUpdating method:
ListViewItem editItem = AdminUsersListView.EditItem;
Guid userId = new Guid((editItem.FindControl("UserId") as HiddenField).Value);
Hashtable dataUpdate = new Hashtable
{
{ "UserName", Request[ (editItem.FindControl("UserNameNew") as TextBox).UniqueID ] },
{ "Email", Request[ (editItem.FindControl("Email") as TextBox).UniqueID ] },
{ "IsApproved", Request[ (editItem.FindControl("IsApproved") as CheckBox).UniqueID ] == "on" },
{ "IsLockedOut", Request[ (editItem.FindControl("IsLockedOut") as CheckBox).UniqueID ] == "on" }
};
var x1 = dataUpdate["UserName"]; // this is corrent new value from Request
var x2 = (editItem.FindControl("UserNameNew") as TextBox).Text; // this is WRONG! OLD! value from TextBox ... Why???
using (Entities entities = new Entities())
{
aspnet_Membership membershipItem = entities.aspnet_Membership.Where(MBS => MBS.UserId == userId).FirstOrDefault();
membershipItem.Email = dataUpdate["Email"].ToString();
membershipItem.LoweredEmail = membershipItem.Email.ToLower();
membershipItem.IsApproved = Convert.ToBoolean(dataUpdate["IsApproved"]);
membershipItem.IsLockedOut = Convert.ToBoolean(dataUpdate["IsLockedOut"]);
entities.SaveChanges();
aspnet_Users userItem = entities.aspnet_Users.Where(USR => USR.UserId == userId).FirstOrDefault();
userItem.UserName = dataUpdate["UserName"].ToString();
userItem.LoweredUserName = userItem.UserName.ToLower();
entities.SaveChanges();
}
AdminUsersListView.EditIndex = -1;
AdminUsersListView.DataSource = _getDataList();
AdminUsersListView.DataBind();
Thanks, Art