在服务器端验证表单

发布于 2024-12-02 02:37:16 字数 137 浏览 1 评论 0原文

我正在 asp.net 4.0 中工作 有没有什么方法可以在服务器端检查,如果我们离开页面并将控件重定向到另一个页面,然后检查当前表单,是否有任何字段被修改?如果是,则先保存该记录,然后重定向到新页面。 有什么方法可以在服务器端检查这个吗? (代码隐藏文件)

I am working in asp.net 4.0
Is there any way to check on server side that if we are leaving the page and redirecting the control to another page, then check on the current form, is there any field that is modified? If yes, then first save that record and then redirect to the new page.
Is there any way to check this on server side? (Code behind file)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

毁梦 2024-12-09 02:37:16

啊,我明白了。作为一个例子,假设您有一个用户类,您将使用该用户类

public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Address { get; set; }
    }

在页面加载事件中显示相关页面表单,并使用相关值加载该对象,并且它们使用加载的对象来显示页面表单。让我们将此对象称为“LoadedUser”

用户单击重定向链接后,您需要处理该事件(我知道您在做什么),并在此事件中使用页面表单上的当前值创建一个新的 User 对象,然后让我们调用此对象对象“NewLoadedUser” 现在我们将使用 IEquatable 接口将 LoadedUser 与 NewLoadedUser 对象进行比较。

因此,继续将其添加到用户类中

public class User: IEquatable<User>
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Address { get; set; }

    public override int GetHashCode()
    {
        return Id ^ Id.GetHashCode(); // or whatever
    }

    public override bool Equals(object other)
    {
        return this.Equals(other as User);
    }

    public bool Equals(User other)
    {
        return (other != null &&
                other.Id == this.Id &&
                other.Username == this.Username &&
                other.Address == this.Address );
    }
}

在代码中执行此操作后,您应该能够像这样比较两个对象。

bool areEqual = NewLoadedUser.Equals(LoadedUser);

然后您可以使用 areEqual bool 标志来更新或不更新记录。

希望这有帮助

Ah I see. As an example lets assume you have a User Class which you will use to display your page form in question

public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Address { get; set; }
    }

on page load event load this object with relevent values and they use the loaded object to display the page form. Lets call this object "LoadedUser"

After the user click on the redirect link you need to handle that event (which I know your doing) and in this event create a new User object using the current values on the page form and then lets call this object "NewLoadedUser" now we are going to compare the LoadedUser with NewLoadedUser object by using IEquatable interface.

So go ahead and add this to the user class

public class User: IEquatable<User>
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Address { get; set; }

    public override int GetHashCode()
    {
        return Id ^ Id.GetHashCode(); // or whatever
    }

    public override bool Equals(object other)
    {
        return this.Equals(other as User);
    }

    public bool Equals(User other)
    {
        return (other != null &&
                other.Id == this.Id &&
                other.Username == this.Username &&
                other.Address == this.Address );
    }
}

After doing this in your code you should be able to compare the two objects like this.

bool areEqual = NewLoadedUser.Equals(LoadedUser);

and then you can use the areEqual bool flag to update the record or not.

Hope this helps

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