wayne on April 24, 2008
I recently, in fact just now ran into an issue formatting a phone. So I don't lose this info I'm writing this post.
The problem was with formatting the phone number output into a repeater control, so I'm dealing with the DataBinder object. Here's the DataBinder statement:
<%#FormatPhoneNumber(DataBinder.Eval(Container.DataItem, "Phone1").ToString())%>
So let's break this apart a bit. FormatPhoneNumber is a function I found from Jason's blog comments so for prosperity I'm going to include it here also:
public string FormatPhoneNumber(string pNum)
{
try
{
if (pNum.Length == 10)
return String.Format("{0:(###) ###-####}", Int64.Parse(pNum));
else if (pNum.Length == 7)
return String.Format("{0:###-####}", Int64.Parse(pNum));
}
catch
{
//
}
return "";
}
I changed it around a little just get er to work. I'll have to swing back to that later. Anyway, continuing on we have our standard DataBinder.Eval call. Another gotcha I had was the .ToString() on the end. When you have null items in your datasource, you'll throw an error without casting the value to a string. Silly. Perhaps now I won't forget. Thanks for taking part in my brain fart.