Formatting Data in a Bound Repeater

Discussion in 'ASP .Net Web Controls' started by Fred Sawtelle, Nov 25, 2003.

  1. Okay, I've wasted a day researching and experimenting
    with no success. Would someone tell me why the code
    below doesn't format properly, and how I can get it to,
    please? This is in the ItemTemplate of a repeater, and
    it displays the bound data just fine, except that it
    displays as 3334445555, just as it's stored in the
    database. I want it to display as (333) 444-5555.

    <%# DataBinder.Eval(Container.DataItem, "VoicePhone", "{0:
    (###) ###-####}") %>

    I have the same situation with a label control on a
    different webform: the value displays, but not formatted.

    <%# DataBinder.Eval(DsDetails1, "Tables
    [DepartmentDetails_get].DefaultView.[0].VoicePhone", "{0:
    (###) ###-####}") %>
     
    Fred Sawtelle, Nov 25, 2003
    #1
    1. Advertisements

  2. Fred Sawtelle

    Guest Guest

    Okay, boys 'n' girls, here's the fix. The problem is
    that we're trying to format a number into a string, but
    the source is not being seen as a number. Do a
    Convert.ToInt64 and all will be well. You can do this
    inside your bound repeater, but not using the repeater's
    built-in formatting; you have to encase the whole
    Databinder.Eval statement within a Convert.ToInt64 and
    then encase that in a String.Format statement.

    Unfortunately, even that craps out if the Phone field of
    your database allows nulls. Convert.ToInt64 craps out on
    a null.

    So what I wound up doing was giving up on the databinding
    and coding serverside to test and assign value:

    If (drv("VoicePhone") Is DBNull.Value) Then
    lblDeptPhone.Text = ""
    ElseIf Trim(drv("VoicePhone")) = "" Then
    lblDeptPhone.Text = ""
    Else
    lblDeptPhone.Text = String.Format("{0:(###) ###-
    ####}", Convert.ToInt64(drv("VoicePhone")))
    End If
     
    Guest, Nov 28, 2003
    #2
    1. Advertisements

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. Alex
    Replies:
    0
    Views:
    548
  2. =?Utf-8?B?RGFu?=

    Formatting date of bound data in datalist

    =?Utf-8?B?RGFu?=, Sep 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    397
    =?Utf-8?B?RGFu?=
    Sep 16, 2004
  3. sunil panda

    Lower bound & Upper bound

    sunil panda, Dec 25, 2003, in forum: Java
    Replies:
    9
    Views:
    7,878
    thushara wijeratna
    Oct 7, 2008
  4. Rhiner Dan
    Replies:
    1
    Views:
    982
    Mike Wahler
    Mar 27, 2005
  5. Mario Krsnic
    Replies:
    0
    Views:
    572
    Mario Krsnic
    Jun 23, 2006
  6. mark4asp
    Replies:
    1
    Views:
    723
    mark4asp
    Dec 3, 2007
  7. Nathan Sokalski

    Getting the data bound to a Repeater

    Nathan Sokalski, Dec 6, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    378
    Milosz Skalecki [MCAD]
    Dec 6, 2007
  8. Nathan Sokalski

    Getting the data bound to a Repeater

    Nathan Sokalski, Dec 6, 2007, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    249
    Milosz Skalecki [MCAD]
    Dec 6, 2007
Loading...