PIA
PIA

Reputation: 1

How to Find Id of a label present inside a gridview

I have a label present inside grid view i want to find the control inside a button click without using

foreach (GridViewRow row in MyGridView.Rows)
{          
    System.Web.UI.WebControls.Label lblName = row.FindControl("lblName") as System.Web.UI.WebControls.Label;
    lblName.Text = "Name";
}

Is there any alternative way to find the control

Upvotes: 0

Views: 710

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460058

I assume that you don't want to loop all rows but that you want to find the label in the same row where the button was clicked. Then use the button's NamingContainer which is the GridViewRow:

protected void button_Click(Object sender, EventArgs e)
{
    Button btn = (Button) sender;
    GridViewRow row = (GridViewRow) btn.NamingContainer;
    Label lblName = (Label) row.FindControl("lblName");
    lblName.Text = "Name";
}

Upvotes: 4

Related Questions