Microsoft Developer
Microsoft Developer

Reputation: 5459

Customize label text in grid view template field

I want to show "N/A" text in grid view label if value is not available in database and if it is available, then the value should be displayed instead of "N/A".

How can I customize my label?

This is the code that I have written to get the value.

<asp:Label ID="lblCineRunFrom" runat="server" Text='<%# Eval("CineRunFrom") %>'></asp:Label>

Upvotes: 0

Views: 5324

Answers (3)

Icarus
Icarus

Reputation: 63956

This works:

<asp:Label id="dada" runat="server" Text='<%# string.Format("{0}",string.IsNullOrEmpty(Eval("CineRunFrom").ToString())?"N/A":Eval("CineRunFrom")) %>' ></asp:Label>

Upvotes: 4

Nalaka526
Nalaka526

Reputation: 11464

Add a new function in code behind & call it from HTML code, check sample code below.

Code

Private Function GetDisplayText(ByVal CineRunFrom As String) As String
      'Do whatever you want here and return text to dispaly as required
End Function

HTML

<asp:Label ID="lblCineRunFrom" runat="server" Text='<%# GetDisplayText(Eval("CineRunFrom")) %>'></asp:Label>

Upvotes: 0

IUnknown
IUnknown

Reputation: 22448

You may use this: Text='<%# Eval("CineRunFrom")?? "N/A" %>'

Upvotes: 0

Related Questions