检查 ASP.net 表单中的更改

发布于 2024-09-13 10:57:03 字数 149 浏览 1 评论 0原文

我想检查我的 ASP.NET 网页上的表单是否有任何更改,我有哪些选择?

我应该检查视图状态是否已更改,或者应该在代码隐藏中创建一个标志,由 Webcontrol 事件(如文本框的 TextChanged 或下拉列表的 SelectedIndexChanged)触发?

I want to check if there have been any changes to a form on my ASP.NET webpage, What are my options?

Should i check if the viewstate has changed or should create a flag in the code-behind, triggered by webcontrol events like TextChanged for Textboxes or SelectedIndexChanged for Dropdownlists?

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

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

发布评论

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

评论(3

檐上三寸雪 2024-09-20 10:57:03

您可以将发送的值存储在属性中。类似于:

Textbox1.Text = <Sent Text>
Textbox1.Attributes.Add "OldText",Textbox1.Text

在回发时,您可以比较:

If Textbox1.Text <> Textbox1.Attributes("OldText") Then
   ' Text is different

您必须对表单中的每个控件执行此操作。当然,您可以编写一个过程以更自动的方式执行此操作,例如迭代所有控件。

You could store the sent values in attributes. Something like:

Textbox1.Text = <Sent Text>
Textbox1.Attributes.Add "OldText",Textbox1.Text

On postback, you can compare:

If Textbox1.Text <> Textbox1.Attributes("OldText") Then
   ' Text is different

You would have to do that for every control in your form. Of course, you could write a procedure to do this in a more automatic way, like iterating through all your controls.

○闲身 2024-09-20 10:57:03

简单的方法:提交该表单,然后在服务器端将发送的值与存储在数据层中的值进行比较。

Easy way: submit that form, and at server-side, compare sent values with those stored in your data layer.

猫瑾少女 2024-09-20 10:57:03

为所有控件设置适当的 OnChange 事件(对于某些控件有所不同,即 Droplist:OnSelectedIndexChanged),以调用单个 form_Changed 函数。在该函数中,将全局变量设置为 true。然后在按钮单击处理程序中检查该值。所有“Changed”事件在按钮单击处理程序之前触发。

ASPX

<asp:CheckBox runat="server" id="loginallowed" Checked="true" OnCheckedChanged="form_Changed" />
<asp:TextBox ID="tbFirst" runat="server" CssClass="form-control required" OnTextChanged="form_Changed"/>

ASPX.CS

private Boolean formChanged = false;
protected void form_Changed(object sender, EventArgs e)
{
    formChanged=true;
}
protected void btn_Click()
{
    if(!formChanged) return;
}

Set the proper OnChange event (different for some controls, i.e. Droplist:OnSelectedIndexChanged) for all controls to call a single form_Changed function. In that function, set a global variable to true. Then in the Button Click Handler, check that value. All "Changed" events trigger before the button Click handler.

ASPX

<asp:CheckBox runat="server" id="loginallowed" Checked="true" OnCheckedChanged="form_Changed" />
<asp:TextBox ID="tbFirst" runat="server" CssClass="form-control required" OnTextChanged="form_Changed"/>

ASPX.CS

private Boolean formChanged = false;
protected void form_Changed(object sender, EventArgs e)
{
    formChanged=true;
}
protected void btn_Click()
{
    if(!formChanged) return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文