If statements in asp repeater

Discussion in 'ASP .Net Web Controls' started by Joachim Feldt, Oct 28, 2004.

  1. Hi,

    To begin with, I should say that I am a beginner in the .Net world..

    I am using asp:repeater in a aspx.page like this:
    <asp:Repeater id="rptMyLabBooks" runat="server">
    <ItemTemplate>
    <tr>
    <td><%# DataBinder.Eval(Container.DataItem, "name")%></td>
    <td><%# DataBinder.Eval(Container.DataItem,
    "start_date","{0:d}")%></td>
    <td><asp:Image id="imgIconOpen" runat="server"
    ImageUrl="Images/icon_open.gif"></asp:Image></td>
    <td><asp:HyperLink id="lnkEditLabBook"
    ImageUrl="Images/icon_edit.gif" runat="server"
    NavigateUrl="editlabBook.aspx"></asp:HyperLink></td>
    <td><asp:HyperLink id="lnkViewLabBook"
    ImageUrl="Images/icon_view.gif" runat="server"
    NavigateUrl="viewlabBook.aspx"></asp:HyperLink></td>
    </tr>
    </ItemTemplate>

    <HeaderTemplate>
    <table cellpadding="3" cellspacing="0" border="0" width="740">
    <tr>
    <td class="listHeader" width="190">Author</td>
    <td class="listHeader" width="100">Start date</td>
    <td class="listHeader" width="40" align="center">Status</td>
    <td class="listHeader" width="40" align="center">Edit</td>
    <td class="listHeader" width="40" align="center">View</td>
    </tr>
    </HeaderTemplate>

    <FooterTemplate>
    </table>
    </FooterTemplate>

    </asp:Repeater>

    No I need to have a couple of if statements here, I do not know how to
    do that?
    One statement would be in the column "Status". Something like this:

    If myDataSet("status") = "Open" Then
    sImage = "icon_open.gif"
    ElseIf myDataSet("status") = "Closed" Then
    sImage = "icon_closed.gif"
    Else
    sImage = "icon_archived.gif"
    End If

    How can I do this? Where do I put it, in Code behind?

    I would be really happay if someone could give me an example!

    Thanks in advance!

    /Jocke
    Joachim Feldt, Oct 28, 2004
    #1
    1. Advertising

  2. Joachim, you can use a "helper function" in the Template. You can see
    an example here:
    http://datawebcontrols.com/faqs/CustomizingAppearance/CustomizeColumnValue.shtml

    This example uses a TemplateColumn in a DataGrid, but the concepts apply
    to templates in DataLists and Repeaters as well.

    Happy Programming!




    Joachim Feldt wrote:
    > Hi,
    >
    > To begin with, I should say that I am a beginner in the .Net world..
    >
    > I am using asp:repeater in a aspx.page like this:
    > <asp:Repeater id="rptMyLabBooks" runat="server">
    > <ItemTemplate>
    > <tr>
    > <td><%# DataBinder.Eval(Container.DataItem, "name")%></td>
    > <td><%# DataBinder.Eval(Container.DataItem,
    > "start_date","{0:d}")%></td>
    > <td><asp:Image id="imgIconOpen" runat="server"
    > ImageUrl="Images/icon_open.gif"></asp:Image></td>
    > <td><asp:HyperLink id="lnkEditLabBook"
    > ImageUrl="Images/icon_edit.gif" runat="server"
    > NavigateUrl="editlabBook.aspx"></asp:HyperLink></td>
    > <td><asp:HyperLink id="lnkViewLabBook"
    > ImageUrl="Images/icon_view.gif" runat="server"
    > NavigateUrl="viewlabBook.aspx"></asp:HyperLink></td>
    > </tr>
    > </ItemTemplate>
    >
    > <HeaderTemplate>
    > <table cellpadding="3" cellspacing="0" border="0" width="740">
    > <tr>
    > <td class="listHeader" width="190">Author</td>
    > <td class="listHeader" width="100">Start date</td>
    > <td class="listHeader" width="40" align="center">Status</td>
    > <td class="listHeader" width="40" align="center">Edit</td>
    > <td class="listHeader" width="40" align="center">View</td>
    > </tr>
    > </HeaderTemplate>
    >
    > <FooterTemplate>
    > </table>
    > </FooterTemplate>
    >
    > </asp:Repeater>
    >
    > No I need to have a couple of if statements here, I do not know how to
    > do that?
    > One statement would be in the column "Status". Something like this:
    >
    > If myDataSet("status") = "Open" Then
    > sImage = "icon_open.gif"
    > ElseIf myDataSet("status") = "Closed" Then
    > sImage = "icon_closed.gif"
    > Else
    > sImage = "icon_archived.gif"
    > End If
    >
    > How can I do this? Where do I put it, in Code behind?
    >
    > I would be really happay if someone could give me an example!
    >
    > Thanks in advance!
    >
    > /Jocke



    --

    Scott Mitchell

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], Oct 28, 2004
    #2
    1. Advertising

  3. Thanks a lot! I think this will work.
    Can I have this function(PrintGender in his example) in the code
    behind instead? If so, how can I access it from the html mode?

    Thanks again!

    /Joachim
    Joachim Feldt, Oct 29, 2004
    #3
  4. Joachim Feldt wrote:
    > Thanks a lot! I think this will work.
    > Can I have this function(PrintGender in his example) in the code
    > behind instead? If so, how can I access it from the html mode?


    Sure, you can have it in the code-behind class for the page. Just make
    sure to make the method Protected (or Public), like:

    Protected PrintGender(gender as String) as String
    ...
    End Function

    hth

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], Oct 29, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,158
    mfmehdi
    Oct 20, 2006
  2. Replies:
    0
    Views:
    836
  3. Harry George
    Replies:
    6
    Views:
    358
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    731
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    248
    John Crichton
    Jul 12, 2010
Loading...

Share This Page