views:

127

answers:

3

I am trying to display a number, stored in a dataset as a string, as a phone number. I have a label that Binds to a value but doesn't display the format that I passed as an arg:

<asp:Label ID="lbl005108002" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "phone number", "{0:(###) ###-####}") %>'></asp:Label>

As a test I tried to pass the formated string into the dataitem but that didn't work either. Is there something else I need to do in order to use this function?

m_Row("phone number") = String.Format("{0:(###) ###-####}", value)

The value displays in both cases as: 04152543926

+4  A: 

I prefer to do the databinding in my codebehind:

<asp:Label ID="lbl005108002" runat="server" Text="" OnDataBinding="lbl005108002_DataBinding"></asp:Label> 

In codebehind:

protected void lbl005108002_DataBinding(object sender, System.EventArgs e)
{
    Label lbl = (Label)(sender);
    lbl.Text = String.Format("{0:(###) ###-####}", (int)(Eval("phone number")));
}

I think the key is that cast to int so that the formatter knows how to deal with the value you are sending it. I prefer doing this in codebehind because sometimes trying to cram everything inline makes things less explicit.

Kelsey
Thanks, it seems I had to convert it to an int first and that is what I was not doing
MemphisDeveloper
A: 

What type is value? This seems to format ok for me:

     int phone_num = 1231231234;
     string f = String.Format("{0:(###) ###-####}", phone_num);
SwDevMan81
A: 

An alternative way is:

Dim i as Integer = 5551234567 Dim s As String = i.ToString("(###) ###-####")

The ToString is the fast way I use. What I did is in th eCommand window I did this:

? 5551234567.ToString("(###) ###-####")

The result was:

"(555) 123-4567"

So I am sure it works. Good luck!

Miroslav Jeliaskoff