Microsoft KB Archive/323261

= How to find child controls that are located in the template of a parent control =

Article ID: 323261

Article Last Modified on 5/12/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



This article was previously published under Q323261





SUMMARY
To find child controls that are located in the template of a parent control, you can use the FindControl method or the Cells collection index. However, the TemplateColumn column and the BoundColumn column are different when you try to use the FindControl method or the Cells collection index to reference a particular control in the cells of a parent control.   In a BoundColumn column, the cell always contains a single control. Therefore, you can use 0 as the index. For example, 0 is the index in the following code: string ProductName = ((TextBox)e.Item.Cells[3].Controls[0]).Text;   In a TemplateColumn column, the controls are interspersed with literal controls. Therefore, the previous blank space makes up a literal control. In this case, the FindControl method must be used with the ControlID parameter as shown in the following code: bool Discon=((CheckBox)e.Item.FindControl(&quot;ControlID&quot;)).Checked; Note The ControlID placeholder is the id of the control. 



MORE INFORMATION
To use the FindControl method or the Cells collection index to find a child control in a parent control, follow these steps:  Start Microsoft Visual Studio .NET Create a new Microsoft ASP.NET Web application project that is named FindControl by using Microsoft Visual C# .NET. By default, WebForm1.aspx is created.  To add a DataGrid control to the WebForm1.aspx page, replace the existing code with the following code: <%@ Page language=&quot;c#&quot; Codebehind=&quot;WebForm2.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;FindControl.WebForm2&quot; %> <HTML> <HEAD> WebForm1 </HEAD> <body MS_POSITIONING=&quot;GridLayout&quot;> <form id=&quot;WebForm2&quot; method=&quot;post&quot; runat=&quot;server&quot;> <asp:datagrid id=&quot;MyDataGrid&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot; OnEditCommand=&quot;MyDataGrid_Edit&quot; OnCancelCommand=&quot;MyDataGrid_Cancel&quot; OnUpdateCommand=&quot;MyDataGrid_Update&quot; OnDeleteCommand=&quot;MyDataGrid_Delete&quot; DataKeyField=&quot;ProductID&quot;> <Columns> <asp:EditCommandColumn ButtonType=&quot;LinkButton&quot; UpdateText=&quot;Update&quot; CancelText=&quot;Cancel&quot; EditText=&quot;Edit&quot;> </asp:EditCommandColumn> <asp:ButtonColumn Text=&quot;Delete&quot; CommandName=&quot;Delete&quot; ></asp:ButtonColumn> <asp:BoundColumn DataField=&quot;ProductID&quot; ReadOnly=&quot;True&quot; HeaderText=&quot;ProductID&quot;></asp:BoundColumn> <asp:BoundColumn DataField=&quot;ProductName&quot; HeaderText=&quot;ProductName&quot;></asp:BoundColumn> <asp:TemplateColumn HeaderText=&quot;Discontinued&quot;> <ItemTemplate> <asp:CheckBox id=Discontinued runat=&quot;server&quot; Checked='<%# DataBinder.Eval(Container.DataItem, &quot;Discontinued&quot;) %>'> </asp:CheckBox> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid> </HTML> </li>  To add the DataGrid_Update and the DataBind methods, replace the existing code in the WebForm1.aspx.cs file with the following code: using System; using System.Collections; using System.Data; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient;

namespace FindControl {   ///     /// Summary description for WebForm2. ///    public class WebForm2 : System.Web.UI.Page {     protected System.Web.UI.WebControls.DataGrid MyDataGrid; DataSet ds = new DataSet; private void Page_Load(object sender, System.EventArgs e)     { SqlConnection myConnection = new SqlConnection(&quot;server=databaseserver;uid=userid;pwd=pwd;database=northwind;&quot;); SqlDataAdapter myCommand = new SqlDataAdapter(&quot;select * from Products&quot;, myConnection); myCommand.Fill(ds, &quot;Products&quot;); MyDataGrid.DataSource=ds.Tables[&quot;Products&quot;].DefaultView; MyDataGrid.DataBind; }     protected void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)       { string ProductName = ((TextBox)e.Item.Cells[3].Controls[0]).Text; bool Discon=((CheckBox)e.Item.FindControl(&quot;Discontinued&quot;)).Checked; Response.Write(&quot;'ProductName'=&quot;+ProductName+&quot; ||'Discontinued' status = &quot;+Discon.ToString+&quot;&quot;); BindGrid; // This is the place to add code to update the database. }      protected void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e) {} protected void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e) {} protected void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)     { MyDataGrid.EditItemIndex = e.Item.ItemIndex; BindGrid; }

void BindGrid {        MyDataGrid.DataSource = ds.Tables[&quot;Products&quot;].DefaultView;; MyDataGrid.DataBind; }       #region Web Form Designer generated code override protected void OnInit(EventArgs e)       { //           // CODEGEN: This call is required by the ASP.NET Web Form Designer. //           InitializeComponent; base.OnInit(e); }       ///         /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///        private void InitializeComponent {           this.Load += new System.EventHandler(this.Page_Load); }       #endregion } } Note Modify the connection string with your database server details. </li> On the Debug menu, click Start to run the application. Note The Web browser window is shown with values in the DataGrid control.</li> In the Web browser window, in the DataGrid control, click the Edit link of the first row. The Update and the Cancel links appear.</li> Click the Update link. Notice that the name of the ProductName field and the status of the Discontinued field appear at the top of the browser window.</li></ol>

<div class="references_section">