Formatting Data in a Bound Repeater

F

Fred Sawtelle

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:
(###) ###-####}") %>
 
G

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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top