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.

 

Leetcode全求和问题

目录 1、编号1 Two Sum Given an array of integers, find two numbers such that they add up to a specifi...
  • wangxiaojun911
  • wangxiaojun911
  • 2014年02月04日 10:29
  • 9981

Leetcode解题笔记(Array)

源码见github https://github.com/Kelvinmao/Leetcode/tree/master/Array 2016-08-08更新154.Find Minimum in Ro...
  • kelvinmao
  • kelvinmao
  • 2016年07月23日 22:38
  • 1565

Leetcode全数组问题

目录 1、编号2 Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respe...
  • wangxiaojun911
  • wangxiaojun911
  • 2014年02月06日 03:32
  • 6334

How to make a winform MessageBox display at center of parent control

1.CbtHook.cs: using System; using System.Text; using System.Runtime.InteropServices; namespac...
  • u014174955
  • u014174955
  • 2014年11月14日 12:39
  • 985

Method to achieve repositioning/resizing of child controls (

  • 2006年02月23日 09:05
  • 35KB
  • 下载

How to extract images from child windows of running applications

  • 2008年12月27日 17:36
  • 37KB
  • 下载

How to: Host Controls in Windows Forms DataGridView Cells(介绍如何在datagridview中自定义列)

The DataGridView control provides several column types, enabling your users to enter and edit valu...
  • jzt_designer
  • jzt_designer
  • 2013年04月27日 10:32
  • 885

How to Use OLE in Rich Edit Controls

This section contains information about using object linking and embedding (OLE) in rich edit contro...
  • achellies
  • achellies
  • 2011年06月24日 22:36
  • 790

#375 – 将Tooltip 于父控件的某个属性绑定(Binding Something in a Tooltip to a Property on the Parent Control)

你可以直接将简单的将控件的某个属性和Tooltip 进行绑定。但是如果Tooltip 的内容是自定义的的,如何使用数据绑定呢? 假设你的Tooltip 的内容中包含了若干个Label,而你想其中某个L...
  • Libby1984
  • Libby1984
  • 2016年10月26日 09:56
  • 380

Simple application that shows how to use the Data Control to

  • 2006年02月23日 09:05
  • 1KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to find child controls that are located in the template of a parent control
举报原因:
原因补充:

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