

Navigator : Home > Tutorials > Controls Tutorials > ...
Locate Controls using ASP.NET 2.0 and VB.NET
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 |
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.
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 |
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" /> <br /> <hr /> <br /> <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> <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.
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 |
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.
Looking for the C# 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!