How can I retrieve the form elements on serverside in dotnet

related article from odetocode


<form id="Form1" method="post" runat="server">
  <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
  <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>

private void Button1_Click(object sender, System.EventArgs e)
{
   TextBox b = Page.FindControl("TextBox1") as TextBox;
   if(b != null)
   {
      Response.Write("Found TextBox1 on Button1_Click<br>");
   }         
}

<input name="ctl00$aspireContent$wellGrid$ctl01$wellName" 
type="text" 
id="ctl00_aspireContent_wellGrid_ctl01_wellName" 
style="display:block;" />

My original name I gave to this text control is "wellname".

wonder if there is a way to force a name outside a naming container

To locate a control by name you need to know the hierarchy of controls

wonder if findcontrol can traverse for a unique in all its children?


private Control FindControlRecursive(Control root, string id) 
{ 
    if (root.ID == id)
    { 
        return root; 
    } 

    foreach (Control c in root.Controls) 
    { 
        Control t = FindControlRecursive(c, id); 
        if (t != null) 
        { 
            return t; 
        } 
    } 

    return null; 
}

The above is borrowed from

Viewstate and dynamically added controls