DotNet Tutorials

Server Intellect

 Locate Controls using ASP.NET 2.0 and C#

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 chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.


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();
}

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>

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();
}
}

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.




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!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect