GridView编辑,双击问题

8

我正在使用GridView,并遇到了点击两次编辑链接才能看到编辑字段的问题。根据建议,在.RowEditing处理程序中重新绑定GridView。问题仍然存在,只有在第二次单击任何编辑链接后才能看到编辑字段。

<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeBehind="Default.aspx.vb" Inherits="GridViewTest._Default" %>

    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <h2>
            Welcome to ASP.NET!
        </h2>
        <p>
            To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
            <asp:GridView ID="gvReport" runat="server" AutoGenerateColumns="False" 
                AutoGenerateEditButton="True">
                <Columns>
                    <asp:BoundField DataField="c1" HeaderText="C1" />
                    <asp:BoundField DataField="c2" HeaderText="C2" />
                    <asp:BoundField DataField="c3" HeaderText="C3" />
                    <asp:BoundField DataField="c4" HeaderText="C4" />
                    <asp:BoundField DataField="c5" HeaderText="C5" />
                    <asp:BoundField DataField="c6" HeaderText="C6" />
                    <asp:BoundField DataField="c7" HeaderText="C7" />
                    <asp:BoundField DataField="c8" HeaderText="C8" />
                </Columns>
            </asp:GridView>
        </p>
        <p>
            You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
                title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
        </p>
    </asp:Content>





    Public Class _Default
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                loaddata()
            End If
        End Sub

        Sub loaddata()

        'Get dataview dvAgTarRet_gv



            gvReport.DataSource = dvAgTarRet_gv
                    gvReport.DataBind()
            Session.Add("gvReport", dvAgTarRet_gv)

            end sub

1
您需要发布后台代码以展示您如何进行数据绑定。听起来您正在不当地将数据绑定到GridView上并丢失了视图状态,因此事件无法与原始控件状态相关联。请尝试仅在页面不处于回发模式下使用Page.Postback进行绑定。 - Brian Scott
Brian,好的,我现在有一个空的.RowEditing处理程序。在单击编辑按钮引起页面回发后,我没有看到任何文本框或页面更改? - fran
Fran,你需要发布你的代码/标记以供审查。 - Brian Scott
代码已添加。@Brian Scott - fran
2个回答

22

找到了。需要设置GridView的EditIndex,然后执行数据绑定。

Private Sub gvReport_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvReport.RowEditing
    gvReport.DataSource = CType(Session("gvReport"), DataView)
    gvReport.EditIndex = e.NewEditIndex
    gvReport.DataBind()
End Sub

在我阅读的100个关于这个问题的“解决方案”中,这是唯一有效的一个。谢谢Fran。 - AnotherDeveloper
1
+1 确实有效,可以根据自己的代码添加特定的数据绑定方法。 - bonCodigo

5

只需要调用您的网格绑定函数:

protected void GvEmployee_RowEditing(object sender, GridViewEditEventArgs e)
{
    GvEmployee.EditIndex = e.NewEditIndex;
    bindgridview();  // method of binding gridview
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接