RE: Sections in repeater

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi,

Your solution is fine.

What I meant to do the grouping after the databinding is completed is
demonstrated using following code:

This is the business object:
====================

Public Class Responsibility
Private m_activityStart As DateTime
Public Property ActivityStart() As DateTime
Get
Return m_activityStart
End Get
Set(ByVal value As DateTime)
m_activityStart = value
End Set
End Property
Private m_person As String
Public Property Person() As String
Get
Return m_person
End Get
Set(ByVal value As String)
m_person = value
End Set
End Property
Private m_responsibilityTypeName As String
Public Property ResponsibilityTypeName() As String
Get
Return m_responsibilityTypeName
End Get
Set(ByVal value As String)
m_responsibilityTypeName = value
End Set
End Property

Public Sub New(ByVal activityStart As DateTime, ByVal person As String,
ByVal responsibilityTypeName As String)
m_activityStart = activityStart
m_person = person
m_responsibilityTypeName = responsibilityTypeName
End Sub

End Class


This is the web form:
================

<form id="form1" runat="server">
<div>
<asp:Repeater ID="repResponsibility" runat="server">
<ItemTemplate>
<div class="adSection">
<asp:Literal ID="litSection"
runat="server"></asp:Literal></div>
<div class="adRow">
<asp:Literal ID="litRow"
runat="server"></asp:Literal></div>
</ItemTemplate>
</asp:Repeater>
</div>
</form>


This is the code-behind of the web form:
===============================

Protected Sub repResponsibility_ItemDataBound(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
repResponsibility.ItemDataBound
Dim r As Responsibility

Dim litSection As Literal
Dim litRow As Literal

Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
r = CType(e.Item.DataItem, Responsibility)

'Point out controls
litSection = CType(e.Item.FindControl("litSection"),
Literal)
litRow = CType(e.Item.FindControl("litRow"), Literal)

'Set values
litSection.Text = r.ActivityStart.ToString("dddd d MMMM")
litRow.Text = String.Format("{0}, {1}", r.Person,
r.ResponsibilityTypeName)
End Select
End Sub

Private Sub GroupBySections()
Dim litSection As Literal
Dim litRow As Literal

Dim prevSection As String = String.Empty
Dim currentSection As String

For Each ri As RepeaterItem In Me.repResponsibility.Items
If (ri.ItemType = ListItemType.AlternatingItem Or ri.ItemType =
ListItemType.Item) Then
litSection = CType(ri.FindControl("litSection"), Literal)
litRow = CType(ri.FindControl("litRow"), Literal)

currentSection = litSection.Text
If currentSection <> prevSection Then
prevSection = currentSection
Else
litSection.Visible = False
End If
End If
Next
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim list As New List(Of Responsibility)
Dim dt1 As DateTime = DateTime.Now
list.Add(New Responsibility(dt1, "test1", "test"))
list.Add(New Responsibility(dt1, "test2", "test"))
Dim dt2 As DateTime = dt1.AddDays(1)
list.Add(New Responsibility(dt2, "test3", "test"))
list.Add(New Responsibility(dt2, "test4", "test"))
Me.repResponsibility.DataSource = list
Me.repResponsibility.DataBind()

GroupBySections()
End Sub



As you can see from above code, actually we only need to set the literal's
Visible property to False to exclude it from the output. You can verify
that the <div> of the hidden section doesn't exists in the html source.

Anyway, I believe this is only two different approach to the same objective.

Let me know if you need further information on this. Thanks.

Regards,
Walter Wang (wawang@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.



.



Relevant Pages

  • Project Error
    ... Private Declare Sub Sleep Lib "Kernel32" ... Dim strDataSrc As String ...
    (microsoft.public.vb.bugs)
  • Re: Is there a way to prevent a RichTextBox from scrolling?
    ... Private _isRegex As Boolean ... Public Sub New(ByVal thispattern As String, ... Dim entry As tDict ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Excel Listing tool using VB
    ... Sub ListFiles2() ... Dim directories() As String, CurrentDirectory As String ... Dim dirtopaste, dirok ...
    (microsoft.public.vb.general.discussion)
  • Form Error
    ... SMSDS_CallerID As String ... Private Declare Sub Sleep Lib "kernel32" ... Dim ComString As String ... Dim AppPath As String, FreeFileNo% ...
    (microsoft.public.vb.bugs)
  • Re: Encrypt/hide Password
    ... Public Sub New(ByVal strCryptoName As String) ... ' instantiated crypto class. ... Dim fsKey As New FileStream(strSaveToPath, FileMode.OpenOrCreate, _ ...
    (microsoft.public.scripting.wsh)