【问题标题】:Gridview - Cannot get updated cell value for RowUpdatingGridview - 无法获取 RowUpdating 的更新单元格值
【发布时间】:2011-11-18 22:51:41
【问题描述】:

我正在使用 ItemTemplate 和 EditTemplate 来编辑网格视图。 (ASP.net + VB)。

我点击编辑按钮,然后我可以选中/取消选中这些复选框并修改文本框的值。 当点击 UPDATE 按钮时,会触发 RowUpdating 事件,但是我发现当我获取更新语句的值时,它仍然获取编辑前的值,而不是更新值。

我怎样才能获得最新和更新的价值?谢谢。 乔

以下是VB代码:

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)

    'Update the values.
    Dim row = Gridview1.Rows(e.RowIndex)

    Dim Col1_SL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_SL"), CheckBox)
    Dim Col1_VL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_VL"), CheckBox)
    Dim Col1_ML = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_ML"), CheckBox)
    Dim Col1_PH = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_PH"), CheckBox)
    Dim Col1_APH = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_APH"), CheckBox)
    Dim Col1_TOIL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_TOIL"), CheckBox)
    Dim Col1_Others = CType(Gridview1.Rows(e.RowIndex).FindControl("tb1_Others"), TextBox)
    Dim Col1_RosterKey = CType(Gridview1.Rows(e.RowIndex).FindControl("lb1_rosterkey"), Label)

    Using conn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("hris_shiftdutyConnectionString").ConnectionString)
        conn.Open()
        cmd.Connection = conn
        sql = "SET DATEFORMAT dmy;UPDATE troster SET SL='" & Convert.ToInt32(Col1_SL.Checked) & "' where roster_key='" & Col1_RosterKey.Text & "';"
        cmd.CommandText = Sql
        reader = cmd.ExecuteReader()
        conn.Close()
        reader.Close()
    End Using

    'Reset the edit index.
    Gridview1.EditIndex = -1

    'Bind data to the GridView control.
    BindData()
End Sub

【问题讨论】:

    标签: asp.net vb.net gridview


    【解决方案1】:

    最可能的原因是这个。
    您在 Page_Load 上调用 BindData() 而不使用 !IsPostBack

    Protected Sub Page_Load Handles Me.Load
        If Not IsPostBack
            ' Bind Grid only at the first load
            ' Do not load Grid again at Postbacks
            BindData()
        End If
    End Sub
    

    【讨论】:

    • 嗨,我在 page_load 上停止调用 bindata() 后收到此错误消息“索引超出范围。必须为非负数且小于集合的大小。参数名称:索引” for !IsPostBack 运行这段代码时出现错误:Dim row = Gridview1.Rows(e.RowIndex)
    【解决方案2】:

    你有两个选择:

    1. 有一个单独的 RowUpdated 方法在绑定到网格视图的数据源执行更新后触发。此方法将包含编辑和插入的新值。
    2. RowUpdating 方法应该有一个GridViewEventArgs 类型的参数。这将有一个名为 NewValues 的属性,其中包含新值。在您的示例中,这是变量 e

    【讨论】:

    • 嗨,对于选项 2,我如何使用 NewValues 函数? e.NewValues ?谢谢
    • e.NewValues 是一种字典类型,因此您可以像 col1 = e.NewValues["BindingName"] 一样使用它,其中 BindingName 是该列的数据绑定名称。
    • oic。但是如何指示行索引以获取更新的单元格值?
    • 您不必...该事件每行触发一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多