我在 gridview 上应用编辑功能。
当我的代码控件处于 rowupdating 事件中时,我发现我正在从文本框中获取以前的值(即在文本框中进行编辑之前的值)。
以下是我的代码:
Protected Sub gvBooking_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvBooking.RowUpdating
'Dim name As String = DirectCast(gvBooking.Rows(e.RowIndex).FindControl("txtperson1"), TextBox).Text
Dim nname As String = (gvBooking.DataKeys(e.RowIndex).Values("person_name").ToString)
Dim id As Integer = Integer.Parse(gvBooking.DataKeys(e.RowIndex).Value.ToString)
Dim carname As String = gvBooking.DataKeys(e.RowIndex).Values("car_name").ToString
Dim carac As String = ""
'Dim sql As String = ""
'sql = "update dbo.tbl_Book set person_name='" + name + "',ac_type='" + carac + "' where booking_id = " + id.ToString
'gc.ExecuteCommand(sql)
'gvBooking.EditIndex = -1
'gc.BindGridView(gvBooking, "select * from tbl_Book")
End Sub
在此,如果我将人名从 Henry 编辑为 Scott,然后按更新按钮,然后我通过调试器发现 nname 仅具有值 Henry,这是我进行编辑之前的值,而不是它应该具有值 Scott这是我编辑的。
我不明白我在哪里犯了错误。
请帮帮我。
最佳答案
点击更新后,会引发两个事件。
1.) GridView.Updating :在即将提交更新之前引发。 在这种情况下,数据库中的数据仍未更新。基本上,此事件是为了在 Update 方法实际将更改的数据提交到数据库之前应用一些动态更改(如果需要)。
2.) GridView.Updated 更新数据后立即引发此事件。因此,只有在这种情况下,您才能放置调试符号并检查值。您将看到您的新值:Scott。
<asp:GridView ID="GridView1" runat="server" OnRowUpdated="GridView1_RowUpdated"></asp:GridView>
因为您在未进行更新时检查数据。这就是为什么您只获取原始值的原因。在 GridView1_RowUpdated() 方法中仅检查新值。
此外,如果您想检查更新事件中的新值,请使用 GridViewUpdateEventArgs.NewValues Property [ e.Newvalues ] 为: [ 检查 MSDN这里]
string s = e.NewValues[1].ToString(); // 2nd Column // c# sample
关于asp.net - 从 gridview 中的文本框获取之前的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17714845/