How to loop a recordset and display html with conditions

  • Thread starter Gabriel Lozano-Morán
  • Start date
G

Gabriel Lozano-Morán

I'm stuck with this one in .NET, I have done this a million of times in
classic ASP and I have to say honestly that I have no clue on how to do this
correctly in ASP.NET.

<%
.....
Set RsItem = Conn.Execute("EXEC blablabla")
Response.Write("<table>")
Do Until RsItem.EOF
activeFlag = RsItem("Active")
If (activeFlag = 2) Then
Response.Write("<tr class=dark>")
Else
If (activeFlag = 3) Then
Response.Write("<tr class=light>")
Else
Response.Write("<tr class=medium>")
End If
End If

Response.Write(" <td>" & RsItem("Name") & "</td>")
Response.Write("</tr>")

RsItem.MoveNext
Loop
....
%>

I need to know how this can be done using the datalist or repeater control.
I'm stuck on implementing the if statements.

Thanks for any help or a lead in the correct direction

Gabriel
 
C

Cactus Corp.

Hello Gabriel,

Here's an example:


// info about 'People', a BASIC class
// notice the REQUIRED properties if you want to directly
// reference them while using any datalisting server control.
class People
{

private string _name;
private string _gender;

public string Name
{
get {return this._name;}
}
public string Gender
{
get {return this._gender;}
}

}



-------------- document.aspx------------------------
<script runat="server">

//returns Mr. or Mrs. wether he/she is a man/woman
private string GetContent(object aGender, object aName)
{
if(Convert.ToString(aGender).Equals("m))
return("Mr. "+aName);
else
return("Mrs. "+aName);
}

private void Page_Load(dummy signature)
{
// get the items , wether from a business object layer or directly through a
// datareader
Arraylist alPeople = People.FindAllPeople(People.OrderBy.Name);

//associate to the repeater
rpItems.DataSource = alPeople;
rpItems.DataBind();
}

</script>

<asp:Repeater ID="rptItems" Runat="server" EnableViewState="False">
<ItemTemplate>
<%# GetContent(DataBinder.Eval(Container.DataItem, "Gender"), Container.DataItem, "FirstName")) %>
</ItemTemplate>
</asp:Repeater>


Antoine, cc
 
C

Cactus Corp.

Actually I forgot to mention what is conceptually done.
The repeater gives you an opportunity to fully customize
the way you want your data displayed.

You should not insert business logic into the repeater
definition as the repeater itself is designed to 'DISPLAY'
information and not make 'DECISIONS' about it.

That is the reason you should instead only have display
statements in the repeater, optionnaly 'function results'
as in my recent example but nothing else.

Hope this helps...

antoine, cc
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,010
Latest member
MerrillEic

Latest Threads

Top