2011-09-07 45 views
0

我正在开发一个Web应用程序,我正在使用GridView控件从数据库加载数据。在asp.net中检查gridview是否脏?

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

P.S.我正在使用LoginView作为注销按钮。

+1

您需要提供更多信息。例如:你如何获取数据并将其绑定到gridview?您是使用SQLDataSource还是使用DAL检索和绑定数据?根据具体情况,您的问题的答案会有所不同。如果可以,请邮编。标记和代码在后面。 – Icarus

+0

我正在使用SQLDataSource加载GridView。 – Macnique

回答

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.

0

我使用简单的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'; 
     } 
    } 
}; 

这也将工作更改到GridView,将提醒用户。我觉得这个方法最好,因为它适用于所有的东西,我不需要为不同的控件编写不同的方法。