E-mail List Archives
Number of posts in this thread: 2 (In chronological order)
From: Martin, Lainie
Date: Mar 22, 2004 11:58AM
Subject: ASP.Net repeater controls and labeling
No previous message | Next message → 
Has anyone found a good way to label repeater controls in ASP.Net?
 
I have a header row with "label for" tags around the corresponding
column header.
Then below that I've got the data repeating for 15 rows down.
 
But because of the nature of repeaters, the id for each corresponding
text box dynamically generates at run-time.  So you can't correspond the "label for" with the "id" at design time.
 
Any suggestions on how to best handle this?
 
Thanks - Lainie
From: Keith Patton
Date: Mar 22, 2004 2:33PM
Subject: RE: ASP.Net repeater controls and labeling
← Previous message | No next message
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
