Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - Locate Controls using ASP.NET 2.0 and C#
Locate Controls using ASP.NET 2.0 and C#

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

ASP.NET Controls Tutorial

This tutorial will show you how to Locate Controls by ID using ASP.NET 2.0 and C#

Every container control on the page, and the page itself, has a Controls collection that you can use to get to individual controls or loop through the controls collection.  On the other way, the ASP.NET page framework provides your applications with automatic control ID resolution through the INamingContainer interface, which generates a naming container for each class that implements it. A naming container defines a new ID namespace within an ASP.NET Web page control hierarchy. A naming container then allows the page framework to generate a value for the UniqueID property of each Control object generated within that namespace. The UniqueID property is different from the ID property that you declare in that it is the fully qualified identifier for a control.

This tutorial only using the default namespace. But Label control will be bound to a data source and the Repeater control iterates , in this tutorial. So,  you will need to import the System.Text, System.Collections namespace.

using System.Text;
using System.Collections;

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

We use the Button1_Click event to loop through all textbox controls in this page. And we use the Button2_Click to find individual controls and print out those controls UniqueID.

protected void Button1_Click(object sender, EventArgs e)
{
string allTextBoxValues = "";
foreach (Control c in Page.Controls)
{
foreach (Control childc in c.Controls)
{
if (childc is TextBox)
{
allTextBoxValues += ((TextBox)childc).Text + ",";
}
}
}
if (allTextBoxValues != "")
{
Label1.Text = allTextBoxValues;
}
}

protected void Button2_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();

ArrayList a = new ArrayList();
a.Add("A");
a.Add("B");
a.Add("C");

MyDataList.DataSource = a;
MyDataList.DataBind();

for (int i = 0; i < MyDataList.Controls.Count; i++)
{
Label l =
(Label)((RepeaterItem)MyDataList.Controls[i]).FindControl("MyLabel");
sb.Append("Container: " +
((RepeaterItem)MyDataList.Controls[i]).NamingContainer.ToString() +
"<p>");
sb.Append("<b>" + l.UniqueID + "</b><p>");
}
ResultsLabel.Text = sb.ToString();
}

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

The front end .aspx page looks something like this:

<table width="500" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" class="basix" style="width: 500px; border-top-style: solid; border-right-style: solid; border-left-style: solid; height: 50px; border-bottom-style: solid">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="ResultsLabel" runat="server" AssociatedControlID="MyDataList" />&nbsp;<br />
<hr />
&nbsp;<br />
&nbsp;<asp:TextBox ID="TextBox1" runat="server" >test1</asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" >test2</asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" >test3</asp:TextBox><br />
<br /><asp:Repeater ID="MyDataList" runat="server" >
<ItemTemplate>
<asp:Label ID="MyLabel" runat="server" Text="<%# Container.DataItem.ToString() %>"></asp:Label><br />
</ItemTemplate>
</asp:Repeater><br>
<br>
&nbsp;
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button1" />
<asp:Button ID="Button2" runat="server" Text="Button2" /></td>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
string allTextBoxValues = "";
foreach (Control c in Page.Controls)
{
foreach (Control childc in c.Controls)
{
if (childc is TextBox)
{
allTextBoxValues += ((TextBox)childc).Text + ",";
}
}
}
if (allTextBoxValues != "")
{
Label1.Text = allTextBoxValues;
}
}

protected void Button2_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();

ArrayList a = new ArrayList();
a.Add("A");
a.Add("B");
a.Add("C");

MyDataList.DataSource = a;
MyDataList.DataBind();

for (int i = 0; i < MyDataList.Controls.Count; i++)
{
Label l =
(Label)((RepeaterItem)MyDataList.Controls[i]).FindControl("MyLabel");
sb.Append("Container: " +
((RepeaterItem)MyDataList.Controls[i]).NamingContainer.ToString() +
"<p>");
sb.Append("<b>" + l.UniqueID + "</b><p>");
}
ResultsLabel.Text = sb.ToString();
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks