ListView 内的输入不会将 ItemUpdating 事件上的旧值更改为最近设置的值

发布于 2024-10-09 21:01:53 字数 2258 浏览 4 评论 0原文

如果有人帮助我理解这种情况,我将不胜感激。 我不知道为什么,但是当我编辑选定的 ListView 项目(包含几个文本框),然后在 ItemUpdating 事件中按“更新”按钮 时,我总是得到旧值,而不是最近输入的值。 为什么?

  1. 我不使用 Page_Load 事件,所以我不需要检查 PostBack
  2. 我尝试在将数据从 DB 绑定到 ListView 之前获取值,所以它不能覆盖最近输入的值
  3. 我尝试在其中获取 TextBoxes 值不同的事件处理程序 - ItemCommand、ItemUpdating、ItemDataBound - 结果始终相同
  4. Collection NewValues 和 OldValues 始终为空(我认为这是因为我不使用 SqlDataSource 控件)
  5. 我可以获得新值的唯一一种方法 - 是检查 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?

  1. I do not use Page_Load event so i do not need check on PostBack
  2. I try to get value before i bind data from DB to ListView, so it can't override recently typed values
  3. I tried to get TextBoxes values in different Event handlers - ItemCommand, ItemUpdating, ItemDataBound - result si always the same
  4. Collection NewValues and OldValues are always empty (i think this is because i don't use SqlDataSource control)
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2024-10-16 21:01:53

我知道答案...F***ING VIEWSTATE!

请注意这一点!
要始终获取实际数据,您需要将 EnableViewState 设置为“False”。

<%@ Page EnableViewState="False"
         Language="C#" 
         MasterPageFile="~/App_Masters/Admin.Master" 
         AutoEventWireup="true" 
         CodeBehind="Users.aspx.cs" 
         Inherits="WebShop.Admin.Users" %>

诀窍在于,仅针对特定输入设置此值是不够的!仅当我在 ASPX 中为整个页面设置此设置后,这才对我有帮助。

I know the answer ... F***ING VIEWSTATE!!!

Be aware of this!
To get data always actual you need to set EnableViewState to "False".

<%@ Page EnableViewState="False"
         Language="C#" 
         MasterPageFile="~/App_Masters/Admin.Master" 
         AutoEventWireup="true" 
         CodeBehind="Users.aspx.cs" 
         Inherits="WebShop.Admin.Users" %>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文