当我在GridView中单击ImageButton时,为什么我没有收到任何数据

时间:2013-01-24 13:40:15

标签: asp.net vb.net gridview

我有以下Gridview,最后一列有一个ImageButton。我试图从单击按钮的行返回名字和姓氏。我花了几个小时看这里和其他网站试图让它工作,但没有成功。

如果有人可以查看我的代码,看看我做错了什么,我们将不胜感激。

由于

ASP代码

<asp:GridView runat="server" ID="gvSecondaryContacts" AutoGenerateColumns="False" DataKeyNames="ContactID" ShowHeaderWhenEmpty="false" GridLines="None" OnRowCommand="gvSecondaryContacts_OnRowCommand"  >
    <Columns>
        <asp:BoundField DataField="ContactID" HeaderText="ContactID" InsertVisible="False" ReadOnly="True" SortExpression="ContactID" Visible="false"/>

        <asp:TemplateField HeaderText="First Name">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtSecFirstName" Text='<%# Eval("FirstName") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Last Name">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtSecLastName" Text='<%# Eval("LastName") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Email">
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtSecEmail" Text='<%# Eval("Email") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Position">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("ClubPosition") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtSecClubPosition" Text='<%# Eval("ClubPosition") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>
            <ItemTemplate>                   
                <asp:ImageButton runat="server" ID="ibtnDelete" CommandName="Delete" AlternateText="Delete Contact" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"/>
            </ItemTemplate>
        </asp:TemplateField>


    </Columns>

VB代码

Public Sub gvSecondaryContacts_OnRowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    If e.CommandName = "Delete" Then
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim row = gvSecondaryContacts.Rows(index)
        Dim ContactID As Integer = Convert.ToInt32(gvSecondaryContacts.DataKeys(index).Value)
        Dim sFirstName As String = gvSecondaryContacts.Rows(index).Cells(1).Text
        Dim sLastName As String = gvSecondaryContacts.Rows(index).Cells(2).Text
        MsgBox("Your name is " & sFirstName & " " & sLastName)
    End If

End Sub

我还为ImageButton尝试了以下操作,但它返回是一种类型,不能用作表达式错误

        <asp:TemplateField>
            <ItemTemplate>                   
                <asp:ImageButton runat="server" ID="ibtnDelete" CommandName="Delete" AlternateText="Delete Contact" CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>"/>
            </ItemTemplate>
        </asp:TemplateField>

3 个答案:

答案 0 :(得分:0)

试试这个

    Dim sFirstName As String = ((Label)gvSecondaryContacts.Rows(index).Cells(1).FindControl("Label2")).Text
    Dim sLastName As String =((Label)gvSecondaryContacts.Rows(index).Cells(2).FindControl("Label1")).Text

答案 1 :(得分:0)

我认为问题是设置CommandArgument of ImageButton

CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>"

根据此MSDN Example,您没有这样做。此外,您(如果从服务器端完成数据绑定)仅在页面未回发时才绑定您的gridview;

If Not Page.IsPostBack Then
   'Bind your gridview here
End if

答案 2 :(得分:0)

您可以处理点击事件。您可以在点击事件处理程序中将ImageButon's NamingContainer转换为GridViewRow。然后你只需要使用FindControl

Protected Sub Delete(sender As Object, e As EventArgs)
    Dim ctrl = DirectCast(sender, Control)
    Dim row = DirectCast(ctrl.NamingContainer, GridViewRow)
    ' you should rename this to LblFirstName
    Dim Label2 = DirectCast(row.FindControl("Label2"), Label)
    ' you should rename this to LblLastName
    Dim Label1 = DirectCast(row.FindControl("Label1"), Label)
    'MessageBoxes in ASP.NET don't make much sense
    MsgBox("Your name is " & Label2.Text & " " & Label1.Text)
End Sub