Solved

Griview Template field fail due to empty string ASP.NET

Posted on 2013-01-11
3
500 Views
Last Modified: 2013-01-11
Hello experts,

I have a template field in gridview, I'm formating the data to Phone#, if the data is null or empty my template field fails.
<asp:TemplateField>
<HeaderTemplate>
 Phone Number</HeaderTemplate>
<HeaderStyle HorizontalAlign="Center" Width="75px" />
 <ItemTemplate>
<asp:Label ID="lblPHNumber" runat="server" Text='<%# String.Format("{0:000-0000}",Convert.ToInt64(DataBinder.Eval(Container.DataItem, "PHN_NUMBER")))%>' /></ItemTemplate>
</asp:TemplateField>

Open in new window

How  can I fix this.

Thanks,
ASPDEV
0
Comment
Question by:ASPDEV
3 Comments
 
LVL 12

Expert Comment

by:Jared_S
ID: 38767821
Can you create a function that evaluates if PHN_NUMBER is null or empty and return nothing if true, and return the formatted string if false?

then just bind myFunction("PHN_NUMBER")
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 500 total points
ID: 38767951
Try this,
<asp:Label ID="lblPHNumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PHN_NUMBER") !="" ? String.Format("{0:000-0000}",Convert.ToInt64(DataBinder.Eval(Container.DataItem, "PHN_NUMBER"))) : ""%>' />

Open in new window

0
 

Author Closing Comment

by:ASPDEV
ID: 38768108
Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question