Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 323261 - Last Review: May 12, 2007 - Revision: 1.6
How to find child controls that are located in the template of a parent control
This article was previously published under Q323261
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:
- 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="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>
- 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. - On the Debug menu, click
Start to run the application. Note The Web browser window is shown with values in the DataGrid control.
- In the Web browser window, in the DataGrid control, click the Edit link of the first row.
The Update and the Cancel links
appear.
- 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.
For additional information,
click the following article number to view the article in the Microsoft
Knowledge Base:
307860Â
(http://kbalertz.com/Feedback.aspx?kbNumber=307860/
)
ASP.NET data binding overview
For more information, visit the following Microsoft
Developer Network (MSDN) Web sites:
APPLIES TO
- Microsoft ASP.NET 1.1
- Microsoft ASP.NET 1.0
| kbdatabinding kbservercontrols kbhowtomaster KB323261 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate