ListView 内的输入不会将 ItemUpdating 事件上的旧值更改为最近设置的值
如果有人帮助我理解这种情况,我将不胜感激。 我不知道为什么,但是当我编辑选定的 ListView 项目(包含几个文本框),然后在 ItemUpdating 事件中按“更新”按钮 时,我总是得到旧值,而不是最近输入的值。 为什么?
- 我不使用 Page_Load 事件,所以我不需要检查 PostBack
- 我尝试在将数据从 DB 绑定到 ListView 之前获取值,所以它不能覆盖最近输入的值
- 我尝试在其中获取 TextBoxes 值不同的事件处理程序 - ItemCommand、ItemUpdating、ItemDataBound - 结果始终相同
- Collection NewValues 和 OldValues 始终为空(我认为这是因为我不使用 SqlDataSource 控件)
- 我可以获得新值的唯一一种方法 - 是检查 Request ,但在这种情况下,我无法使用控制验证器......所以仅处理请求可能是个坏主意。
这是 ItemUpdating 方法的代码:
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();
谢谢,Art
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道答案...F***ING VIEWSTATE!
请注意这一点!
要始终获取实际数据,您需要将 EnableViewState 设置为“False”。
诀窍在于,仅针对特定输入设置此值是不够的!仅当我在 ASPX 中为整个页面设置此设置后,这才对我有帮助。
I know the answer ... F***ING VIEWSTATE!!!
Be aware of this!
To get data always actual you need to set EnableViewState to "False".
The trick is that setting this just for specific input is not enough! This helped me only after i set this for whole page in ASPX.