【问题标题】:Check is dirty for the gridview in asp.net?检查asp.net中的gridview是否脏?
【发布时间】:2011-09-07 15:09:48
【问题描述】:

我正在开发一个 Web 应用程序,我在其中使用 GridView 控件从数据库加载数据。

我有一行以可编辑模式加载。我有一个保存按钮,用户在进行更改后单击它,我想检查 IsDirty 标志以阻止用户保存并通过警报框通知他们。我正在使用 Web 用户控件 (ASCX) 来加载 GridView。当用户单击保存按钮或注销按钮时,如何检查网格中的脏行并阻止用户保存?

附:我正在使用 LoginView 作为注销按钮。

【问题讨论】:

  • 您需要提供更多信息。例如:您如何获取数据并将其绑定到 gridview?您是使用 SQLDataSource 还是使用 DAL 来检索和绑定数据?根据具体情况,您的问题的答案会有所不同。可以的话发个码。标记和代码隐藏。
  • 我正在使用 SQLDataSource 加载 Gridview。

标签: c# asp.net gridview


【解决方案1】:

您需要处理网格中的 OnRowUpdating 事件,例如:

<asp:GridView id="a" runat="server" OnRowUpdating="a_rowUpdating" ... />

在后面的代码中:

protected void a_rowUpdating (object sender, GridViewUpdateEventArgs e)
{
   //Add logic appropriately to know whether you should allow the update or not.
   //If you shouldn't, just set e.Cancel=true as below:
   e.Cancel=true;//will stop from updating.
}

您可以访问 GridViewUpdateEventArgs 对象中的旧值和新值。更多详情和示例代码,请查看here.

【讨论】:

    【解决方案2】:

    我使用一个简单的 JS 方法来检查 ASP.net 页面中的各种变化。我将它添加到名为 CheckDirty.js 的 JS 文件管理器中,并在页面上添加了指向它的链接,文件中是以下代码:

    var checkDirty = true;
    var form_clean;
    //this should be called when the save button is clicked, but prior to the page post
    function onSave() {
        checkDirty = false;
    }
    
    // serialize clean form
    $(function () {
        form_clean = $("form").serialize();
    });
    
    window.onbeforeunload = function (e) {
        if (checkDirty) {
            var form_dirty = $("form").serialize();
            if (form_clean != form_dirty) {
                return 'Your changes will be lost';
            }
        }
    };
    

    这也适用于更改网格视图并提醒用户。我觉得这个方法最好,因为它适用于所有东西,我不需要为不同的控件编写不同的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 2013-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      相关资源
      最近更新 更多