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


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("ControlID")).Checked; 
    Note The ControlID placeholder is the id of the control.


To use the FindControl method or the Cells collection index to find a child control in a parent control, follow these steps:
  1. Start Microsoft Visual Studio .NET
  2. 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.
  3. To add a DataGrid control to the WebForm1.aspx page, replace the existing code with the following code:
    <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="FindControl.WebForm2" %> <HTML> <HEAD> <title>WebForm1</title> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="WebForm2" method="post" runat="server"> <asp:datagrid id="MyDataGrid" runat="server" AutoGenerateColumns="False" OnEditCommand="MyDataGrid_Edit" OnCancelCommand="MyDataGrid_Cancel" OnUpdateCommand="MyDataGrid_Update" OnDeleteCommand="MyDataGrid_Delete" DataKeyField="ProductID"> <Columns> <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"> </asp:EditCommandColumn> <asp:ButtonColumn Text="Delete" CommandName="Delete" ></asp:ButtonColumn> <asp:BoundColumn DataField="ProductID" ReadOnly="True" HeaderText="ProductID"></asp:BoundColumn> <asp:BoundColumn DataField="ProductName" HeaderText="ProductName"></asp:BoundColumn> <asp:TemplateColumn HeaderText="Discontinued"> <ItemTemplate> <asp:CheckBox id=Discontinued runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "Discontinued") %>'> </asp:CheckBox> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid></form> </body> </HTML> 
  4. 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> /// Summary description for WebForm2. /// </summary> 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("server=databaseserver;uid=userid;pwd=pwd;database=northwind;"); SqlDataAdapter myCommand = new SqlDataAdapter("select * from Products", myConnection); myCommand.Fill(ds, "Products"); MyDataGrid.DataSource=ds.Tables["Products"].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("Discontinued")).Checked; Response.Write("<b>'ProductName'="+ProductName+" ||'Discontinued' status = "+Discon.ToString()+"</b>"); 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["Products"].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); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } } 
    Note Modify the connection string with your database server details.
  5. On the Debug menu, click Start to run the application. Note The Web browser window is shown with values in the DataGrid control.
  6. In the Web browser window, in the DataGrid control, click the Edit link of the first row. The Update and the Cancel links appear.
  7. 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.


个人分类: .net
想对作者说点什么? 我来说一句