我有这样的asp网格:
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" Width="100%" ShowFooter="false" DataKeyNames="ID" Enabled="false" BorderStyle="solid" BorderWidth="1px" BorderColor="#ACA899" BackColor="#BFD3EE" CellSpacing="1" GridLines="none">
<asp:TemplateField HeaderText="ProductID" Visible="false" HeaderStyle- HorizontalAlign="left" HeaderStyle-CssClass="gvheader" HeaderStyle-Font-Bold="true" ItemStyle-Width="40px" >
<ItemTemplate>
<asp:label runat="server" ID="lblProductID" Text='<%#eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" HeaderStyle-HorizontalAlign="left" HeaderStyle-CssClass="gvheader" HeaderStyle-Font-Bold="true" ItemStyle-Width="50px">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnShowProductDetails" OnClick="lnkbtnShowProductDetails_Click" runat="server" Enabled="true">Details</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
在代码背后我有:
Sub lnkbtnShowProductDetails_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
如何获取当前行的ID(datakeynames的值),我不想在这里使用命令和命令参数。
请帮助我!!
答案 0 :(得分:3)
您可以使用以下完整解决方案。
protected void lblTicketNumber_Click(object sender, EventArgs e)
{
LinkButton b = (LinkButton)sender;
GridViewRow row = (GridViewRow)b.NamingContainer;
if (row != null)
{
int rowIndex = row.RowIndex;
string key = LstViewTicket.DataKeys[rowIndex].Value.ToString();
}
}
答案 1 :(得分:2)
您可以使用以下代码访问数据密钥名称
Sub lnkbtnShowProductDetails_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim lnkbtnShowProductDetails As LinkButton = DirectCast(sender, LinkButton)
Dim gridViewRow As GridViewRow = DirectCast(lnkbtnShowProductDetails.NamingContainer, GridViewRow)
Dim val = gv.DataKeys(gridViewRow.DataItemIndex)
Dim key = gv.DataKeyNames(gridViewRow.DataItemIndex)
End Sub