The Code which you pasted in that sender is GridView not CommandField.
You need to use RowCommand Or LinkButton Onclick Eevent
Refer this
HTML
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Select" OnClick="Select" CommandArgument="View profile"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Select(object sender, EventArgs e)
{
LinkButton btn = (sender as LinkButton);
GridViewRow row = btn.NamingContainer as GridViewRow;
if (btn.CommandArgument.Equals("View profile"))
{
//Session["conID"] = (row.FindControl("Label6") as Label).Text;
Response.RedirectPermanent("~/TrainersDetails.aspx", false);
}
else
{
// Session["conID"] = (row.FindControl("Label6") as Label).Text;
Response.RedirectPermanent("~/Marketing/Block Trainers.aspx");
}
}
Also Check the ID of Label in the GridView
Session["conID"] = (row.FindControl("Label6") as Label).Text;