.NET Repeaters Cheat Sheet (vb)

Posted by Zu on March 17, 2010 in .Net |
<asp:Repeater ID=”rpPostTest” runat=”server” OnItemDataBound=”rpPostTest_OnItemDataBound”>
<ItemTemplate>
<asp:Label ID=”lblQuestion” runat=”server”></asp:Label>
<asp:Label ID=”lblQID” visible=”false” runat=”server”></asp:Label>
<br />
<asp:RadioButtonList ID=”answer” runat=”server”  >
<asp:ListItem    Text=”True”  Value=”T” ></asp:ListItem>
<asp:ListItem   Text=”False”  Value=”F”></asp:ListItem>
</asp:RadioButtonList>
<hr />
</ItemTemplate>
</asp:Repeater>

create the repeater

&lt;asp:Repeater ID="rpPostTest" runat="server" OnItemDataBound="rpPostTest_OnItemDataBound"&gt;

&lt;ItemTemplate&gt;

&lt;asp:Label ID="lblQuestion" runat="server"&gt;&lt;/asp:Label&gt;

&lt;asp:Label ID="lblQID" visible="false" runat="server"&gt;&lt;/asp:Label&gt;

&lt;br /&gt;

&lt;asp:RadioButtonList ID="answer" runat="server"  &gt;

&lt;asp:ListItem  Text="True"  Value="T" &gt;&lt;/asp:ListItem&gt;

&lt;asp:ListItem   Text="False"  Value="F"&gt;&lt;/asp:ListItem&gt;

&lt;/asp:RadioButtonList&gt;

&lt;hr /&gt;

&lt;/ItemTemplate&gt;

&lt;/asp:Repeater&gt;

back end code:

databind the repeater itself

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then

rpPostTest.DataSource = objTraining.getRandomQuestions()

rpPostTest.DataBind()

End If

End Sub

databind object inside a repeater with appropriate values

Protected Sub rpPostTest_OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)

If (e.Item.ItemType = ListItemType.Item) OrElse (e.Item.ItemType = ListItemType.AlternatingItem) Then

‘fill in the drop down list

Dim item As RepeaterItem = e.Item

Dim drv As System.Data.DataRowView = DirectCast((e.Item.DataItem), System.Data.DataRowView)

Dim strQuestion As String = drv.Row("question").ToString()

Dim strID As String = drv.Row("id").ToString()

Dim l As Label = DirectCast(item.FindControl("lblQuestion"), Label)

l.Text = strQuestion

Dim l2 As Label = DirectCast(item.FindControl("lblQID"), Label)

l2.Text = strID

End If

End Sub

now… to access objects inside a repeater with an external control:

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

Dim correct As Integer = 0

Dim wrong As Integer = 0

Dim userid As Integer = Session("userID")

For Each repeaterItem As RepeaterItem In rpPostTest.Items

Dim l As Label = DirectCast(repeaterItem.FindControl("lblQID"), Label)

Dim d As RadioButtonList = DirectCast(repeaterItem.FindControl("answer"), RadioButtonList)

Dim dd As DropDownList = DirectCast(repeaterItem.FindControl("ddlAnswer"), DropDownList)

<span style="color: #000000;">//calculations and updates go here
</span>

Next

End Sub

to catch the selectedIndexChange of the dropdownlist

refer here:

http://www.dirigodev.com/blog/post/aspdropdownlist-does-not-fire-onselectedindexchanged-event-in-an-asprepeater.aspx

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2007-2012 Cheat Sheet All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.