How to find child controls that are located in the template of a parent control
2004-07-09 21:12
1061 查看
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: Start Microsoft Visual Studio .NETCreate 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.
相关文章推荐
- How-to find the SQL that using lots of temp tablespace in Oracle
- FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- How to invoke the method of managed bean and render view in JSF when we are outside the lifecycle of JSF
- How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
- R12: How to add Microsoft Excel as Type to the Create Template List of Values in BI Publisher (Doc ID 1343225.1)
- How to make an SDL window the child of another window in Win32
- How to get IOleSite interface of the WebBrowser in an ActiveX control
- How To Call Run() Method of the Microsoft Script Control in C++
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx
- whether the results are the same of getClass().getName() called in child class and parent class?
- Troubleshooting High I/O Wait in Linux --- A walkthrough on how to find processes that are causing h
- [转]How to set the control word of FPU in delphi
- how to reference the parent form from the WPF control(Control in ElementHost)
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.
- In C# how to find the mime type of a file
- how to find out the response code(200,404,400...) of the URL?
- How to fix the issue that GEM_HOME and/or GEM_PATH not set issue for rvm in mac version 10.12
- How to print the content of a Rich Edit Control
- how to enable #ifdef macro in the command line of make?