Repeater and Custom HTML Question

Discussion in 'ASP .Net' started by Arsen V., Dec 25, 2003.

  1. Arsen V.

    Arsen V. Guest

    Hello,

    How to control the structure of the HTML inside of the ItemTemplate of the
    Repeater?

    For example, if a certain data column value is empty (or contains some
    specific string or number), I want to be able to "hide" a part of the HTML
    of the ItemTemplate.

    Say a customer has phone number and extension. The extension is optional.
    When the extension is there, the phone should be displayed in this format:
    123-555-6789 Ext: 123

    If there is no extension, the phone should be simply: 123-555-6789.

    I have the following ItemTemplate:

    <ItemTemplate>
    <tr>
    <td>Phone:
    <%#DataBinder.Eval(Container.DataItem,"phone")%>-x<%#DataBinder.Eval(Contain
    er.DataItem,"extension")</td>
    </tr>
    </ItemTemplate>

    However, this will produce an undesirable output if there is no extension:
    123-555-6788 Ext:

    What is the most appropriate way to avoid this? An example would be great!

    I am sure may people have come across this, since it is very common to have
    "very dynamic" HTML output that is dependent on the values of the data that
    is bound to the Repeater control.

    Thanks,
    Arsen
     
    Arsen V., Dec 25, 2003
    #1
    1. Advertising

  2. Arsen V.

    Jos Guest

    Arsen V. wrote:
    > Hello,
    >
    > How to control the structure of the HTML inside of the ItemTemplate
    > of the Repeater?
    >
    > For example, if a certain data column value is empty (or contains some
    > specific string or number), I want to be able to "hide" a part of the
    > HTML of the ItemTemplate.
    >
    > Say a customer has phone number and extension. The extension is
    > optional. When the extension is there, the phone should be displayed
    > in this format: 123-555-6789 Ext: 123
    >
    > If there is no extension, the phone should be simply: 123-555-6789.
    >
    > I have the following ItemTemplate:
    >
    > <ItemTemplate>
    > <tr>
    > <td>Phone:
    >

    <%#DataBinder.Eval(Container.DataItem,"phone")%>-x<%#DataBinder.Eval(Contain
    > er.DataItem,"extension")</td>
    > </tr>
    > </ItemTemplate>
    >
    > However, this will produce an undesirable output if there is no
    > extension: 123-555-6788 Ext:
    >
    > What is the most appropriate way to avoid this? An example would be
    > great!
    >
    > I am sure may people have come across this, since it is very common
    > to have "very dynamic" HTML output that is dependent on the values of
    > the data that is bound to the Repeater control.
    >
    > Thanks,
    > Arsen


    Write a function like this (this is for VB):

    Function FormatPhoneExt(phone As Object, ext As Object) As String
    If(ext.ToString()<>"")
    Return phone.ToString() & " Ext: " & ext.ToString()
    Else
    Return phone
    End If
    End Function

    Then, change the binding syntax to this:
    <%# FormatPhoneExt(DataBinder.Eval(Container.DataItem,"phone"),
    DataBinder.Eval(Container.DataItem,"extension") %>

    --

    Jos Branders
     
    Jos, Dec 25, 2003
    #2
    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. Replies:
    0
    Views:
    880
  2. Replies:
    0
    Views:
    2,906
  3. Replies:
    1
    Views:
    7,051
    Karl Seguin
    Aug 10, 2005
  4. =?Utf-8?B?SmF5YnVmZmV0?=
    Replies:
    9
    Views:
    860
    Phillip Williams
    Feb 21, 2006
  5. uncensored
    Replies:
    0
    Views:
    832
    uncensored
    Oct 25, 2006
Loading...

Share This Page