A Better way? Finding ASP.NET controls, finding their id

Why not just use the Control.FindControl(string) method?

private void Button1_Click(object sender, EventArgs MyEventArgs)
{
      // Find control on page.
      Control myControl1 = FindControl("TextBox2");
      if(myControl1!=null)
      {
         // Get control's parent.
         Control myControl2 = myControl1.Parent;
         Response.Write("Parent of the text box is : " + myControl2.ID);
      }
      else
      {
         Response.Write("Control not found");
      }
}

from: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.control.findcontrol

Tags:

C#

Asp.Net