1

我有这个代码:

 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

有什么帮助吗?

谢谢你 :)

4

1 回答 1

0

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

GridViewUpdateEventArgs中有两个字典,OldValues并且NewValues. 您可以通过像这样循环它们来找出发生了什么变化:

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

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

于 2013-08-20T07:09:09.447 回答