format phone in a repeater/datalist

T

TJ

The phone number in my dataset is formatted 5558675309.
I present it in the aspx page as <%#
DataBinder.Eval(Container.DataItem,"phone_number") %>
How can I format this to show it like 555-867-5309?
I could do this in code behind no prob,but I'm pretty rough when it comes to
string formatting in the aspx page.
 
S

ScottStoecker

Assuming you can count on the phone number being ten digits long, you
could do something like this:

'Inside the aspx page
<asp:TextBox id=txtPhone
Text='ReturnPhoneNumber(Container.DataItem("phone_number")'
runat=server />

'Inside the code behind file
Protected Function ReturnPhone (Phone As String) As String
Return Phone.Substring(0, 3) & "-" & Phone.Substring(3, 3) & "-" &
Phone.Substring(6)
End Function

Hopefully there are no typos in that.

Scott
 
S

ScottStoecker

Oops, there was:

<asp:TextBox id=txtPhone
Text='ReturnPhoneNumber(Container.DataItem("phone_number"))'
runat=server />
 
T

TJ

Thanks for the very fast response.

Hmm...the textbox displays not the formatted return string from the
function, but rather the exact text we put into the aspx page calling it,
"'ReturnPhoneNumber(Container.DataItem("phone_number"))". Hmmm.

This technique seems valid, thx for pointing me in the right direction. Hope
it doesn't matter that the code is actually in a nested repeater...but w/o
trying to format from a code behind function shows it just fine (just ugly
and unformatted).
 
S

ScottStoecker

Ah, because the delimiters were not put around the call to
ReturnPhoneNumber function:

<asp:TextBox id=txtPhone
Text='<%# ReturnPhoneNumber(Container.DataItem("phone_number")) %>'
runat=server />
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top