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

原创 2004年07月09日 21:12:00

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("ControlID")).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:
  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.

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

Added View has RecyclerView as parent but view is not a real child. Unfiltered index:0

Added View has RecyclerView as parent but view is not a real child. Unfiltered index:0
  • msl0903
  • msl0903
  • 2017年06月22日 18:01
  • 715

BUG:The specified child already has a parent. You must call removeView() on the child's parent first

在使用ViewPager + Fragment 的时候报错:The specified child already has a parent. You must call removeView() o...
  • ss1168805219
  • ss1168805219
  • 2016年08月21日 22:48
  • 797

Android—java.lang.IllegalStateException: The specified child already has a parent. You must call rem

是这样的:界面包含四个Viewpaper,第二个Viewpaper里面有个ListView ListView的每一项,我写一个item.xml文件,这个文件里面有个LinearLayout控件 ...
  • weiguishan
  • weiguishan
  • 2015年04月09日 16:36
  • 925

java.lang.IllegalStateExceptionThe specified child already has a parent

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(...
  • JQ_AK47
  • JQ_AK47
  • 2016年09月08日 16:32
  • 1160

安卓自定义对话框及The specified child already has a child问题

问题:在android开发过程中,有时会在不同情况下遇到同种问题:The specified child already has a parent.You must call removeView(...
  • hemeng2009
  • hemeng2009
  • 2014年10月24日 10:34
  • 778

java.lang.IllegalStateException: The specified child already has a parent

在使用ViewPager的时候遇到一个错误java.lang.IllegalStateException: The specified child already has a parent. You ...
  • android_zhengyongbo
  • android_zhengyongbo
  • 2017年11月17日 12:22
  • 162

C# 控件遍历

遍历控件foreach (Control control in this.Controls) { if (control.GetType() =...
  • jiutao_tang
  • jiutao_tang
  • 2011年07月07日 14:38
  • 4778

当使用ViewPager滑动发生java.lang.IllegalStateException: The specified child already has a parent.异常时的解决方案

这种异常是该控件在前面已经添加过一个父控件,当再次滑动重复执行instantiateItem方法,会重复添加,解决方法是先判断其父容器是否存在,如存在,先和此子控件解除关系 代码如下: @Over...
  • zhou12314
  • zhou12314
  • 2016年03月14日 14:58
  • 1868

freemarker.template.TemplateModelException: Don't know how to present an object of this type to a te

我在页面编写测试list标签代码一直通过不了,总是报freemarker.template.TemplateModelException: Don't know how to present an o...
  • zl544434558
  • zl544434558
  • 2014年11月21日 13:51
  • 2261

ViewPager使用时报错The specified child already has a parent.常见三种解决方法

最近在做公司的一个滚动广告条时使用viewpager来实现,原本觉得很简单的事居然报了个The specified child already has a parent.You must call r...
  • u011339364
  • u011339364
  • 2015年03月09日 11:43
  • 667
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to find child controls that are located in the template of a parent control
举报原因:
原因补充:

(最多只允许输入30个字)