Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
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 VB.NET
Locate Controls using ASP.NET 2.0 and VB.NET


ASP.NET Controls Tutorial

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

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.

Imports System.Text
Imports System.Collections

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

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 Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim allTextBoxValues As String = ""
Dim c As Control
Dim childc As Control
For Each c In Page.Controls
For Each childc In c.Controls
If TypeOf childc Is TextBox Then
allTextBoxValues &= CType(childc, TextBox).Text & ","
End If
Next
Next
If allTextBoxValues <> "" Then
Label1.Text = allTextBoxValues
End If
End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sb As New StringBuilder()
sb.Append("Container: " + _
MyDataList.NamingContainer.ToString() + "<p>")

Dim a As New ArrayList()
a.Add("A")
a.Add("B")
a.Add("C")

MyDataList.DataSource = a
MyDataList.DataBind()

Dim i As Integer
Dim l As Label
For i = 0 To MyDataList.Controls.Count - 1
l = CType(CType(MyDataList.Controls(i), RepeaterItem).FindControl("MyLabel"), Label)
sb.Append("Container: " & _
CType(MyDataList.Controls(i), RepeaterItem).NamingContainer.ToString() & _
"<p>")
sb.Append("<b>" & l.UniqueID.ToString() & "</b><p>")
Next
ResultsLabel.Text = sb.ToString()
End Sub

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

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>

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.

The flow for the code behind page is as follows.

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim allTextBoxValues As String = ""
Dim c As Control
Dim childc As Control
For Each c In Page.Controls
For Each childc In c.Controls
If TypeOf childc Is TextBox Then
allTextBoxValues &= CType(childc, TextBox).Text & ","
End If
Next
Next
If allTextBoxValues <> "" Then
Label1.Text = allTextBoxValues
End If
End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sb As New StringBuilder()
sb.Append("Container: " + _
MyDataList.NamingContainer.ToString() + "<p>")

Dim a As New ArrayList()
a.Add("A")
a.Add("B")
a.Add("C")

MyDataList.DataSource = a
MyDataList.DataBind()

Dim i As Integer
Dim l As Label
For i = 0 To MyDataList.Controls.Count - 1
l = CType(CType(MyDataList.Controls(i), RepeaterItem).FindControl("MyLabel"), Label)
sb.Append("Container: " & _
CType(MyDataList.Controls(i), RepeaterItem).NamingContainer.ToString() & _
"<p>")
sb.Append("<b>" & l.UniqueID.ToString() & "</b><p>")
Next
ResultsLabel.Text = sb.ToString()
End Sub
End Class

Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!

Looking for the C# 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks