Skip to main content

ASP.NET GridView - Format Template column data using format strings and Custom Methods

This article contains GridView template column formatting through Data Format String, ternary operator blocks and custom methods.

Using Format String
Date time


<itemtemplate>
<asp:Label ID="lblDOB" runat="server" Text='<%# Eval("DOB","{0:d}") %>'></asp:Label>
</itemtemplate>
</asp:TemplateField>

For Long Date,

<itemtemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Eval("DOB","{0:d}") %>'></asp:Label>
</itemtemplate>
</asp:TemplateField>

Using ternary operator

<ItemTemplate>
<asp:Image
ID="ImageOpenPending" runat="server"
ImageUrl='<%# Convert.ToInt32(Eval("StateId")) == 1 ? "~/images/icons/DashBoard_Green_Bullet.gif" : "~/images/icons/DashBoard_Red_Bullet.gif" %>' />

<asp:HyperLink ID="HyperLinkWorkOrderSummery" runat="server"
NavigateUrl='<%# "~/AuthUser/WorkOrder_Main.aspx?Id=" + HttpUtility.UrlEncode(DataBinder.Eval(Container.DataItem,"Id").ToString()) + "&mode=View" %>' > <%#Convert.ToDateTime(Eval("StartDate")).ToShortDateString().ToString() + " ," %>
</asp:HyperLink>
</ItemTemplate>

Using a code behind custom method



<ItemTemplate>
<asp:Label ID="LabelPublicComment" runat="server" ToolTip='<%# Eval("PublicComment") %>'
Text='<%# FormatName(DataBinder.Eval(Container.DataItem, "PublicComment")).ToString() %>'>
</asp:Label>
</ItemTemplate>


Write this method in code behind

protected string FormatName(object description)
{
string desc = Convert.ToString(description);
if (desc.Length > 25)
return desc.Substring(0, 25) + "..";
else
return desc;
}

Comments

Popular posts from this blog

Close Parent window from nested iframe child pages : javascript;

for : IE and FireFox 1: <script language="javascript" type="text/javascript"> 2: function CloseAll() { 3: if (top === self) { 4: alert('not iframed'); 5: if (navigator.appName == "Microsoft Internet Explorer") { 6: self.close(); 7: } 8: else { 9: var respnseVal = confirm("Do you wan't to close this window"); 10: if (respnseVal) { 11: netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite"); 12: window.open('', '_self'); 13: window.close(); 14: } 15: } 16: } 17: else { 18: alert('iframed'); 19: if (navigator.appName == "Microsoft Internet Explorer") { 20: top.close(); 21: } 22: else { 23: var respnseVal = confirm("Do you w...

One of my old favorite Games - Z (Strategy)

Z - strategy game i used to love this game , it was quite fascinating strategy game around 1996 download : http://www.486games.net/game.php?id=173