在RowEditing上设置EditIndex时,GridView不会更改为编辑模式

时间:2014-10-30 12:57:26

标签: asp.net vb.net gridview edit

我正在使用Visual Studio 2010,VB.NET,目标框架.NET 4.0。

我有一个GridView,它绑定到一些对象集合,带有一个CommandField列,可以允许编辑所选行。

我正在RowEditing事件处理程序中正确设置EditIndex属性。

问题是:当我点击生成的“编辑”链接时,似乎没有发生任何事情,该行将以“查看模式”再次呈现,而不是“编辑模式”。

但如果我做一些随机回发,比如点击我的“doNothing”按钮,该行将在下一个回发中以“编辑模式”呈现。

我已设法在以下代码中重现该问题:

ASPX:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="MyForm.aspx.vb" Inherits="MySandBox.MyForm" %>

<%@ Register Src="MyControl.ascx" TagName="MyControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="gvInfrator" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:CommandField ShowEditButton="true" />
            <asp:BoundField HeaderText="MyField" DataField="MyField" />
        </Columns>
    </asp:GridView>
    <asp:Button ID="btnDoNothing" runat="server" Text="Just do a postback" />
    </form>
</body>
</html>

代码背后:

Public Class MyDto
    Public Property MyField As String
End Class

Public Class MyForm
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.BindMyData()
    End Sub

    Private Sub BindMyData()
        Dim myData As New MyDto
        myData.MyField = "My field value"

        Me.gvInfrator.DataSource = New MyDto() {myData}
        Me.gvInfrator.DataBind()
    End Sub

    Protected Sub gvInfrator_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvInfrator.RowEditing
        Me.BindMyData() 

        Me.gvInfrator.EditIndex = e.NewEditIndex
    End Sub

End Class

我做错了吗?这是.NET Framework 4中的错误吗?有没有解决方法?

1 个答案:

答案 0 :(得分:3)

在设置GridView的EditIndex后,您需要绑定数据

Protected Sub gvInfrator_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvInfrator.RowEditing
    Me.gvInfrator.EditIndex = e.NewEditIndex
    Me.BindMyData() 
End Sub