GridView更新事件 [英] GridView Updating event

查看:79
本文介绍了GridView更新事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我正在尝试在页面中构建动态gridview,一切正常,直到编辑记录并按Update,然后才使用旧值.
我在更新事件中的代码是:

Hi all
I am trying to build a dynamic gridview in a page, everything is working properly until I edit a record and press Update, when I do, it takes the old values.
my code in the updating event is:

Private Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
    Dim con As New SqlConnection(ConnectionStrings("Test").ToString)
    Try
        Dim row = GridView1.Rows(e.RowIndex)
        Dim tran As SqlTransaction
        Dim da As New SqlDataAdapter
        Dim strENo, strEName, strDob, strSalary, strDNo As String
        strENo = (CType((row.Cells(0).Controls(0)), TextBox)).Text
        strEName = (CType((row.Cells(1).Controls(0)), TextBox)).Text
        strDob = (CType((row.Cells(2).Controls(0)), TextBox)).Text
        strSalary = (CType((row.Cells(3).Controls(0)), TextBox)).Text
        strDNo = (CType((row.Cells(4).Controls(0)), TextBox)).Text
        If con.State = ConnectionState.Closed Then con.Open()
        tran = con.BeginTransaction
        Dim comUpt As New SqlCommand("SP_UPDATE_EMP", con)
        comUpt.CommandType = CommandType.StoredProcedure
        comUpt.Parameters.Add(New SqlParameter("@Emp_no", SqlDbType.Char, 10)).Value = strENo
        comUpt.Parameters.Add(New SqlParameter("@Emp_Name", SqlDbType.VarChar, 50)).Value = strEName
        comUpt.Parameters.Add(New SqlParameter("@DOB", SqlDbType.DateTime, 8)).Value = strDob
        comUpt.Parameters.Add(New SqlParameter("@Salary", SqlDbType.Decimal, 17)).Value = strSalary
        comUpt.Parameters.Add(New SqlParameter("@D_No", SqlDbType.SmallInt, 2)).Value = strDNo
        comUpt.Parameters.Add(New SqlParameter("@ErrNo", SqlDbType.SmallInt)).Direction = ParameterDirection.Output
        comUpt.Transaction = tran
        da.UpdateCommand = comUpt
        da.Update(ds, "emp")
        tran.Commit()
        LoadGrid()
    Catch ex As Exception
    Finally
        If con.State = ConnectionState.Open Then con.Close()
    End Try
End Sub


有人可以建议我该怎么做吗?


can anybody advice me what should I do?

推荐答案

好吧,您每次都在页面加载中加载网格.因此是旧的价值观.使用 IsPostback 属性可以避免这种情况,它应该很好.
Well, you are loading the grid evertime in your pageload. Hence the old values. Use Page IsPostback property to avoid it and it should be good.


这篇关于GridView更新事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆