How to Access ASP.NET Controls Inside An Outer And An Inner ASP Repeaters (Controls Inside Two ASP Repeaters)

Working with ASP.NET repeaters can be a little bit tricky if you do not fully understand them. In plain words, you have to find the outer repeater, then from here find the inner repeater, then from this inner repeater find the control(s). If I haven’t confused you enough, look at the examples, it will be clearer 🙂






This is the ASPX Code

<asp:Repeater ID="rptPerfSts" runat="server">
	<ItemTemplate>
		<p style="font-weight:bold">(<%#Eval("PSID").ToString()%>) <%#Eval("PSName").ToString()%></strong></p>
		<asp:Repeater ID="rptPerfStsInd" runat="server" DataSource='<%#LoadPSIndicators( Eval("PSID") )%>'>
			<ItemTemplate>
				&nbsp;&nbsp;&nbsp;&nbsp;<asp:CheckBox ID="chkPSInd" runat="server" Text='<%#Eval("PSID").ToString() & "-" & Eval("PSIID") & " " & Eval("PSIName") %>' Checked="false" />
				&nbsp;&nbsp;<asp:Label ID="lblPSInd" runat="server" Visible="false" Text='<%#String.Format("{0}-{1}", Eval("PSID"), Eval("PSIID"))%>'></asp:Label> <br />
			</ItemTemplate>
		</asp:Repeater>
	</ItemTemplate>
</asp:Repeater>

These are two methods to access the controls

Method 1: Using two foreach loops (my default choice). In the first loop I call the outer repeater rptPerfSts and iterate thru its items casting them into a RepeaterItem. This will find the inner repeater rptPerfStsInd, and from here I can find the inner repeater controls and again casting them into RepeaterItems. Having already the inner repeater items, we can find the controls inside.
Method 2: Using one foreach loop to find the outer repeater rptPerfSts and iterate thru its items casting them into a RepeaterItem. The second loop is a for loop, you will find the inner repeater rptPerfStsInd and counting the controls inside it. From here you can use the index i to find your inner controls, as indicated below in the example

C# Code

protected void btnSavePS_Click(object sender, EventArgs e)
{
	//Method 1
	foreach (RepeaterItem item in rptPerfSts.Items) {
		foreach (RepeaterItem innerItem in item.FindControl("rptPerfStsInd").Controls) {
			string str = ((Label)innerItem.FindControl("lblPSInd")).Text;
		}
	}

	//Method 2
	foreach (RepeaterItem item in rptPerfSts.Items) {
		for (int i = 0; i <= item.FindControl("rptPerfStsInd").Controls.Count - 1; i++) {
			string str = ((Label)item.FindControl("rptPerfStsInd").Controls.Item(i).FindControl("lblPSInd")).Text;
		}
	}
}

VB.NET code

Protected Sub btnSavePS_Click(ByVal sender As Object, ByVal e As EventArgs)
	'Method 1
	For Each item As RepeaterItem In rptPerfSts.Items
		For Each innerItem As RepeaterItem In item.FindControl("rptPerfStsInd").Controls
			Dim str As String = CType(innerItem.FindControl("lblPSInd"), Label).Text
		Next
	Next

	'Method 2
	For Each item As RepeaterItem In rptPerfSts.Items
		For i As Integer = 0 To item.FindControl("rptPerfStsInd").Controls.Count - 1
			Dim str As String = CType(item.FindControl("rptPerfStsInd").Controls.Item(i).FindControl("lblPSInd"), Label).Text
		Next
	Next
End Sub





Esau Silva
Software Engineer at Region One ESC
Microsoft Full Stack Application Developer
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed.
Share