WebAIM - Web Accessibility In Mind

E-mail List Archives

RE: ASP.Net repeater controls and labeling

for

From: Keith Patton
Date: Mar 22, 2004 2:33PM


Hi Lainie,

One solution would be to use a literal control to output the label using
the databind event of the repeater control:

<!-html -->
<asp:Repeater ID="rpt" Runat="server>"
<ItemTemplate>
<asp:Literal ID="litFor" Runat="server" />
<asp:TextBox ID="txtBox" Runat="server" />
</ItemTemplate>
</asp:Repeater>

'' vb.net code behind file

Protected WithEvents rpt As Repeater

Private Sub rpt_DataBind(ByVal s As Object, ByVal e As
repeaterItemEventArgs) Handles rpt.ItemDataBound
Dim objItemType As ListItemType = CType(e.Item.ItemType,
ListItemType)

If objItemType = ListItemType.Item Or objItemType ListItemType.AlternatingItem Then
Dim txtBox As TextBox CType(e.Item.FindControl("txtBox"), TextBox)
Dim litFor As Literal CType(e.Item.FindControl("litFor "), Literal)
litFor.Text = "<label for=""" & txtBox.ClientID
& """>LABEL NAME</label>"
End If

End Sub


Note that in a normal ASP.NET page it's very easy to use label for with
a server control when you aren't troubled by binding, by doing the
following for example

<!-html-->
<label for="<%= txtBox.ClientID %>">Search Text</label>
<input type="text" id="txtBox" Runat="server" />

'' vb code behind file
Protected WithEvents txtBox As HtmlInputControl

Hope that helps,

Keith Patton

Technical Director
Ethical Media
http://www.ethicalmedia.com