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. Advertising

  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. 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. Alex
    Replies:
    0
    Views:
    420
  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:
    325
    =?Utf-8?B?RGFu?=
    Sep 16, 2004
  3. =?Utf-8?B?RGFu?=

    Re: Formatting date of bound data in datalist

    =?Utf-8?B?RGFu?=, Sep 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,441
    =?Utf-8?B?RGFu?=
    Sep 16, 2004
  4. Mario Krsnic
    Replies:
    0
    Views:
    377
    Mario Krsnic
    Jun 23, 2006
  5. mark4asp
    Replies:
    1
    Views:
    583
    mark4asp
    Dec 3, 2007
Loading...

Share This Page