Home All Groups Group Topic Archive Search About
Author
22 Jan 2006 6:01 PM
bernadou
Ok, this is yet again one of those things that is driving me crazy.  How do I
use a formatting expression to format a number?  Here's what I'm trying, but,
it isn't working.

Text='<%# Eval("fldPhone", "(###)-###-####") %>'

What I'm trying to see happen is the value "3335551212" output as
"(333)-555-1212".  The code above outputs "(###)-###-####".  I'm obviously
way off here, but, I can't seem to track down a straightforward article on
how to do this.  Any help would be appreciated.

Thanks
Bernie

Author
22 Jan 2006 8:50 PM
Phillip Williams
Hi Bernie,

The syntax you tried is correct for numbers, but if your fldPhone is of type
string you would get the outcome that you got instead of the one you
expected.  So to solve it, either convert it to a number before applying this
format string or use a regular expression. 

For example in this line of code I remove any character that might prevent
the field from becoming a number, then convert it to a number then convert it
back again to a string using the format string that you already knew:

<asp:TextBox ID="lblPhone" runat="server" Text='<%#
                Convert.ToInt64(Eval("fldPhone").ToString().Replace(" ",
"").Replace(".", "").Replace("-", "").Replace("(", "").Replace(")",
"")).ToString("000-000-0000")
                 %>'></asp:TextBox>

In the following line I did the same string cleaning process but then used a
regular expression to achieve the same format:

<asp:Label ID="lblPhone" runat="server"
Text='<%#FormatPhone(Eval("fldPhone").ToString()) %>'></asp:Label>

The FormatPhone function is defined as this:

protected  string FormatPhone(string strPhone)
    {
        strPhone =  Convert.ToInt64( strPhone.Replace ("
","").Replace(".","").Replace("-","").Replace ("(","").Replace
(")","")).ToString ("0000000000");
        string ret= Regex.Replace(strPhone,
@"(?<AreaCode>\d{3})(?<seg1>\d{3})(?<seg2>\d{4})",
"(${AreaCode})-${seg1}-${seg2}");
        return ret;
    }

To understand the regular expression syntax you might review this demo on my
website in which I explained the usage of named groups in regular expressions
http://www.webswapp.com/CodeSamples/aspnet20/RegExp_NamedGroups.aspx
Show quoteHide quote
"bernadou" wrote:

> Ok, this is yet again one of those things that is driving me crazy.  How do I
> use a formatting expression to format a number?  Here's what I'm trying, but,
> it isn't working.
>
> Text='<%# Eval("fldPhone", "(###)-###-####") %>'
>
> What I'm trying to see happen is the value "3335551212" output as
> "(333)-555-1212".  The code above outputs "(###)-###-####".  I'm obviously
> way off here, but, I can't seem to track down a straightforward article on
> how to do this.  Any help would be appreciated.
>
> Thanks
> Bernie
Author
23 Jan 2006 1:25 PM
bernadou
Thanks once again!  YTM!  If you ever find yourself in Denver, I owe you a
beer!

Thanks,
B

Show quoteHide quote
"Phillip Williams" wrote:

> Hi Bernie,
>
> The syntax you tried is correct for numbers, but if your fldPhone is of type
> string you would get the outcome that you got instead of the one you
> expected.  So to solve it, either convert it to a number before applying this
> format string or use a regular expression. 
>
> For example in this line of code I remove any character that might prevent
> the field from becoming a number, then convert it to a number then convert it
> back again to a string using the format string that you already knew:
>
> <asp:TextBox ID="lblPhone" runat="server" Text='<%#
>                 Convert.ToInt64(Eval("fldPhone").ToString().Replace(" ",
> "").Replace(".", "").Replace("-", "").Replace("(", "").Replace(")",
> "")).ToString("000-000-0000")
>                  %>'></asp:TextBox>
>
> In the following line I did the same string cleaning process but then used a
> regular expression to achieve the same format:
>
> <asp:Label ID="lblPhone" runat="server"
> Text='<%#FormatPhone(Eval("fldPhone").ToString()) %>'></asp:Label>
>
> The FormatPhone function is defined as this:
>
> protected  string FormatPhone(string strPhone)
>     {
>         strPhone =  Convert.ToInt64( strPhone.Replace ("
> ","").Replace(".","").Replace("-","").Replace ("(","").Replace
> (")","")).ToString ("0000000000");
>         string ret= Regex.Replace(strPhone,
> @"(?<AreaCode>\d{3})(?<seg1>\d{3})(?<seg2>\d{4})",
> "(${AreaCode})-${seg1}-${seg2}");
>         return ret;
>     }
>
> To understand the regular expression syntax you might review this demo on my
> website in which I explained the usage of named groups in regular expressions
> http://www.webswapp.com/CodeSamples/aspnet20/RegExp_NamedGroups.aspx
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "bernadou" wrote:
>
> > Ok, this is yet again one of those things that is driving me crazy.  How do I
> > use a formatting expression to format a number?  Here's what I'm trying, but,
> > it isn't working.
> >
> > Text='<%# Eval("fldPhone", "(###)-###-####") %>'
> >
> > What I'm trying to see happen is the value "3335551212" output as
> > "(333)-555-1212".  The code above outputs "(###)-###-####".  I'm obviously
> > way off here, but, I can't seem to track down a straightforward article on
> > how to do this.  Any help would be appreciated.
> >
> > Thanks
> > Bernie