OnRowCommand没有在GridView上触发

时间:2016-08-16 22:11:03

标签: .net vb.net gridview

我有一个gridview,每行包含一个按钮,我想发布一个事件。单击按钮时,事件不会触发,所有发生的事情都是onLoad事件触发,页面重新加载而不会填充gridview。

.aspx代码如下:

<asp:GridView id="gvUsers" 
        OnRowCommand="gvUsers_RowCommand"
        AutoGenerateColumns="False"
        runat="server">
    <Columns>
        <asp:BoundField DataField="UserID" HeaderText="UserID" />
        <asp:TemplateField>
            <ItemTemplate>
                <%# Eval("FirstName")%> <%# Eval("LastName")%>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>
            <ItemTemplate> 
            <asp:Button ID="lnkSubmit" runat="server" CommandName="Submit" Text="Action" CommandArgument='<%# CType(Container,GridViewRow).RowIndex %>' ></asp:Button> 
            </ItemTemplate> 
        </asp:TemplateField>
    </Columns>
</asp:GridView>

我的aspx.cs如下:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dr As SqlDataReader = db.GetUsers("dlw@fixtureslive.com")
        Dim users As New List(Of FLLib.FixturesLive.Core.User)
        While dr.Read
            Dim u As New FLLib.FixturesLive.Core.User
            u.UserID = dr("UserID")
            u.FirstName = dr("FirstName")
            u.LastName = dr("LastNAme")
            u.Sex = dr("Sex")
            u.EmailAddress = dr("Email")
            users.Add(u)
        End While

        gvUsers.DataSource = users.ToArray
        gvUsers.DataBind()
    End If
End Sub


Sub gvUsers_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    If e.CommandName.Equals("Submit") Then
        Dim oItem As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
        Dim RowIndex As Integer = oItem.RowIndex
    End If
End Sub

任何帮助都将不胜感激。

由于 安迪

1 个答案:

答案 0 :(得分:0)

您已省略Handles gvUsers.RowCommand部分。像这样更改您的事件签名

Sub gvUsers_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvUsers.RowCommand