GridView RowUpdating无法获取新值,它只获取旧值

时间:2013-08-20 06:55:15

标签: vb.net asp.net-mvc-4

我有这段代码:

 Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
    Dim strPersonID As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox1"), TextBox).Text
    Dim s As String=e.NewValues[1].ToString()
    Dim strLastName As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("TextBox2"), TextBox).Text
    Dim command As SqlCommand = New SqlCommand("updatetest", cn)
    command.CommandType = CommandType.StoredProcedure
    command.Parameters.AddWithValue("@pid", strPersonID)
    command.Parameters.AddWithValue("@pfirstname", strLastName)
    If cn.State = ConnectionState.Closed Then
        cn.Open()
    End If
    command.ExecuteNonQuery()
    If cn.State = ConnectionState.Open Then
        cn.Close()
    End If
End Sub

问题是事件rowUpdating返回旧值,那么如何获取更新的新值?请注意,编程没有运行事件RowUpdated

任何帮助?

谢谢你:)

1 个答案:

答案 0 :(得分:0)

当触发RowUpdating事件时,尚未将更改提交回数据源。这是一个旨在让您有机会验证输入并在必要时阻止更改的事件。

GridViewUpdateEventArgs中有两个词典,OldValuesNewValues。你可以像这样循环来弄清楚改变了什么:

For Each key As String In e.OldValues.Keys
    If e.OldValues(key) <> e.NewValues(key) Then
        MessageBox.Show(key & " has changed: Old Value = " & e.OldValues(key) & "; New Value = " & e.NewValues(key))
    End if
Next

那是大脑编译器,但我认为语法是正确的。

相关问题