Formatting the output of a phone number field

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.

permalink Permalink or Trackback Comment Comments (0) Cat ASP.Net
Technorati: No reaction yet!
Tags:
Actions: E-mail

Was this helpful?

If you liked this or found it helpful, please digg it, stumble it, buzz it, whatever it, to say thank you.





Add to Technorati Favorites

 
 If you would like to receive these posts as they happen, you can subscribe to my feed or receive my posts in your email.

Related Posts

Add comment



(Will show your Gravatar icon)  

biuquote
  • Comment
  • Preview
Loading



Check it out mango: Any links must be entered as http://www.somewhere.com with nothing touching it. Anything else will be mangled. This is to help combat spam and to also ensure the masses know of this little tidbit before they click Save comment below. :) I have this down to remind me to do something with it, but I take things slow and easy on the old horse.

Keeps her regular don't ya know, and I wouldn't want to disturb that.



CSS Template by RamblingSoul | Illinois Wine. Adapted to BlogEngine by Wayne John
EatonWeb Blog Directory  Blog Directory Blogger Forum: About Blogging for Bloggers DaniWeb - IT Professionals' Lounge Community