To ensure proper Intellisense and strong typing, put your databinding code in the code file instead of the ASPX page. You will find your apps much easier to support should/when your typed object changes. The only "yellow" you should see in your ASPX page is at the top.
Your repeater should look like this...
<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<div id="mydiv" runat="server" />
</ItemTemplate>
</asp:Repeater>
Your code file should look like this...
Option Explicit On
Option Infer On
Option Strict On
'Replace this class with your custom typed object'
Public Class TypedObject
Public [Class] As String
Public [Name] As String
Sub New(ByVal NewClass As String, ByVal NewName As String)
Me.Class = NewClass
Me.Name = NewName
End Sub
End Class
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Creating sample data and binding it to the repeater'
Dim aData() As TypedObject = {New TypedObject("Class1", "Name1"), New TypedObject("Class2", "Name2")}
rep.DataSource = aData
rep.DataBind()
End Sub
Private Sub rep_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rep.ItemDataBound
'Do not process headers/footers/separators'
Select Case e.Item.ItemType
Case WebControls.ListItemType.Item, WebControls.ListItemType.AlternatingItem
Case Else
Exit Sub
End Select
'Aquire our datasource for this row'
Dim dr = DirectCast(e.Item.DataItem, TypedObject)
'Aquire the control to bind (Do this for each control you are binding)'
Dim mydiv = DirectCast(e.Item.FindControl("mydiv"), HtmlGenericControl)
'bind the control'
mydiv.InnerHtml = dr.Name
mydiv.Attributes("class") = dr.Class
End Sub
End Class
You should also consider using an <asp:Label>
instead of a <div>
. Then you can use the .CssClass
property instead of .Attributes("class")
.