Formatting Repeater Items

W

Wayne Wengert

I have a bound repeater control on my ASP page and am having a problem
figuring out how to format the output for two different fields. The
Subroutine I am trying to create is listed below. The syntax in that sub to
identify the item being processed and exactly how to format it has me
baffled.
The applicable entries in the ItemTemplate are as follows:

<td align="center"><%# DataBinder.Eval(Container,"DataItem.CGClosed") %>
</td>

and

<td><%# DataBinder.Eval(Container,"DataItem.StartDate") %>) </td>

The first item will always return either a 0 or 1 and in the case of 0 I
want to display "No" and in the case of 1 I want to display "Yes"

For the second item, I simply want to display the short date

Pointers to any information will be appreciated.

Wayne

=========== Sub ================
Sub R1_ItemDataBound(ByVal Sender As Object, ByVal e As
RepeaterItemEventArgs)

If (e.Item.ItemType = ListItemType.Item) Or _

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

If CType(e.Item.DataItem,) = "0" Then <== How do I tell it what Item I
want to process/

CType(e.Item.FindControl("CGClosed"), Label).Text = "<b>No</b>"

End If

End If

End Sub
 
K

Karl

Wayne, an alternative solution to using ItemDatabound is to call a sub from
your aspx:

<td align="center"><%#
FormatYesNo(DataBinder.Eval(Container,"DataItem.CGClosed")) %>

and

<td><%# FormatDateTime(DataBinder.Eval(Container,"DataItem.StartDate")) %>)
</td>


sub
protected function FormatYesNo(closed as boolean) as string
if closed then
return "Yes"
end if
return "No"
End function

protected function FormatDateTime(date as DateTime) as string
return date.ToShortDate()
end function


or something similar..
 
D

Davide Vernole [MVP]

Wayne Wengert said:
I have a bound repeater control on my ASP page and am having a problem
figuring out how to format the output for two different fields. The
Subroutine I am trying to create is listed below. The syntax in that
sub to identify the item being processed and exactly how to format it
has me baffled.
The applicable entries in the ItemTemplate are as follows:
<td align="center"><%# DataBinder.Eval(Container,"DataItem.CGClosed")
%> </td>
and
<td><%# DataBinder.Eval(Container,"DataItem.StartDate") %>) </td>

Change this as follow:

<td align="center"><asp:label id="lblCGCClosed" runat="server" /></td>
The first item will always return either a 0 or 1 and in the case of
0 I want to display "No" and in the case of 1 I want to display "Yes"

For the second item, I simply want to display the short date

Pointers to any information will be appreciated.

Wayne

=========== Sub ================
Sub R1_ItemDataBound(ByVal Sender As Object, ByVal e As
[CUT]
End Sub

Sub R1_ItemDataBound(ByVal Sender As Object, ByVal e As
RepeaterItemEventArgs)
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
If (CType(e.Item.DataItem, DataRowView)).Row("CGClosed).ToString() =
"0" Then
CType(e.Item.FindControl("CGClosed"), Label).Text = "<b>No</b>"
Else
CType(e.Item.FindControl("CGClosed"), Label).Text = "<b>Yes</b>"
End If

Dim shortDate as DateTime = DateTime.Parse((CType(e.Item.DataItem,
DataRowView)).Row.("StartDate))
CType(e.Item.FindControl("lblStartDate"), Label).Text =
shortDate.ToShortDateString()
End If
End Sub
 
W

Wayne Wengert

Karl;

Great suggestion. I never even knew you could do that. I remembered seeing a
sample once using the repeater bind event but I can't find it so I was
trying to adapt some code from MSDN. Your approach is much cleaner.

Wayne
 
W

Wayne Wengert

Thanks Davide. That was more along the lines I remembered from that example
I mentioned. Let me experiment with that and see if I can get my head around
the logic and syntax.

Wayne

Davide Vernole said:
Wayne Wengert said:
I have a bound repeater control on my ASP page and am having a problem
figuring out how to format the output for two different fields. The
Subroutine I am trying to create is listed below. The syntax in that
sub to identify the item being processed and exactly how to format it
has me baffled.
The applicable entries in the ItemTemplate are as follows:
<td align="center"><%# DataBinder.Eval(Container,"DataItem.CGClosed")
%> </td>
and
<td><%# DataBinder.Eval(Container,"DataItem.StartDate") %>) </td>

Change this as follow:

<td align="center"><asp:label id="lblCGCClosed" runat="server" /></td>
The first item will always return either a 0 or 1 and in the case of
0 I want to display "No" and in the case of 1 I want to display "Yes"

For the second item, I simply want to display the short date

Pointers to any information will be appreciated.

Wayne

=========== Sub ================
Sub R1_ItemDataBound(ByVal Sender As Object, ByVal e As
[CUT]
End Sub

Sub R1_ItemDataBound(ByVal Sender As Object, ByVal e As
RepeaterItemEventArgs)
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
If (CType(e.Item.DataItem, DataRowView)).Row("CGClosed).ToString() =
"0" Then
CType(e.Item.FindControl("CGClosed"), Label).Text =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top