根据用户单击的行获取gridview中一列的值

时间:2013-10-05 14:27:20

标签: asp.net vb.net gridview linkbutton

我有这段代码,根据用户点击的行,我无法获得学号的值

ASPX

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
            OnSelectedIndexChanged="SelectEvent_click"  HorizontalAlign="Center" DataKeyNames="StudentNumber">
            <Columns>
            <asp:BoundField DataField="StudentNumber" HeaderText="Student Number"/>
            <asp:BoundField DataField="F_name" HeaderText="First name"/>
            <asp:BoundField DataField="L_name" HeaderText="Last name"/>
            <asp:BoundField DataField="Course" HeaderText="Course"/>
            <asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:dd-MMM-yyyy}"/>
            <asp:TemplateField HeaderText="View Application">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkApplication" runat="server" onClick = "SelectEvent_click"
                        Text="Select"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:GridView>

VB

 Protected Sub SelectEvent_click(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
        Label1.Text = GridView1.SelectedValue
        If GridView1.SelectedIndex >= 0 Then
            Response.Write(Label1.Text)
            Session("StudentNumber") = Label1.Text
            Response.Redirect("PCOApplicationView.aspx")
        Else
            Response.Write("Something bad happen please try again later!!")
        End If
    End Sub

1 个答案:

答案 0 :(得分:0)

您可以在按钮点击事件中使用NamingContainer属性,如下所示:

Dim btn As LinkButton = DirectCast(sender, LinkButton)
GridViewRow = DirectCast(btn.NamingContainer, GridViewRow)