Repeater Control Binding question

  • Thread starter Thread starter Donald Williamson
  • Start date Start date
D

Donald Williamson

I have a DataReader that I am binding to a Repeater control. In the
DataReader, I have two fields: 'Name1' and 'Name2'

'Name1' will always have a person's name. Name2 periodically has a person's
name. So the question is if there is a value in 'Name2', I want to display
only that name. Otherwise, display 'Name1'

Any help on how to do this?

Using VS.NET - vb

Thanks in advance.

- Donald
 
Donald said:
I have a DataReader that I am binding to a Repeater control. In the
DataReader, I have two fields: 'Name1' and 'Name2'

'Name1' will always have a person's name. Name2 periodically has a
person's name. So the question is if there is a value in 'Name2', I
want to display only that name. Otherwise, display 'Name1'

Any help on how to do this?

Using VS.NET - vb

Thanks in advance.

- Donald

Write a function

Function GetName(Name1 As String, Name2 As String) As String
If (Name2<>"") Then Return Name2
Return Name1
End Function

Use an ItemTemplate on the Repeater, and add this binding syntax to
the template:

<%# GetName(DataBinder.Eval(Container.DataItem, "Name1"),
DataBinder.Eval(Container.DataItem, "Name2") %>
 
I don't see why everyone uses this DataBinder.Eval thing?
I'm not even sure how it works... Just typecast the
DataItem and use it... it's pretty simple...

for example if you bind a bunch of datarows to a repeater
like this:
DataRow[] rows = sometable.Select(...);
repeater.DataSource = rows;
repeater.DataBind();

and then in the aspx code you can databind like this:
<%# ((System.Data.DataRow)Container.DataItem
["someField"].ToString() %>

no need for some crazy eval syntax, this works just fine...

:) my 2 cents :)
 
Back
Top