Get a label Text or DataKey value on button click in a gridview
Write the .aspx code as below:--
<asp:GridView ID="grdtest" runat="server" AutoGenerateColumns="false"
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblid" runat="server" ></asp:Label>
<asp:Button ID="btnRemove" runat="server" OnClick="btnRemoveclicked" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Write the C# code to get the DataKey value and LabelText
protected void btnRemoveclicked(object sender, EventArgs e)
{
Button button1= (Button)sender;
GridViewRow row = (GridViewRow)button1.Parent.Parent;
labeltext= ((Label)row.FindControl("lblid") as Label).Text;
string string datakey = grdtest.DataKeys[row.RowIndex].Value.ToString();
}
0 Comment(s)