当GridView的数据绑定与事件绑定同时存在时,当进行数据绑定时,页面会触发事件绑定,导致输入的值被改变。解决方法是在数据绑定事件中添加一个条件判断,如果是页面初次打开或者是点击编辑按钮时才进行数据绑定。
示例代码:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then If e.Row.RowState = DataControlRowState.Normal OrElse e.Row.RowState = DataControlRowState.Alternate OrElse e.Row.RowState = (DataControlRowState.Normal OrElse DataControlRowState.Edit) OrElse e.Row.RowState = (DataControlRowState.Alternate OrElse DataControlRowState.Edit) Then e.Row.Attributes.Add("onclick", "javascript:__doPostBack('" + GridView1.UniqueID + "','Edit$" + e.Row.RowIndex.ToString + "')") End If End If End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) GridView1.EditIndex = e.NewEditIndex GridView1.DataBind() '附加条件判断 End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) GridView1.EditIndex = -1 GridView1.DataBind() '附加条件判断 End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) GridView1.EditIndex = -1 GridView1.DataBind() '附加条件判断 End Sub