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

Enable Apache in XAMPP at Windows 7

1. Problem detected! Port 80 in use by "Unable to open process" with PID 4! Apache WILL NOT start without the configured ports free! You need to uninstall/disable/reconfigure the blocking application or reconfigure Apache and the Control Panel to listen on a different port 2. Error: Apache shutdown unexpectedly. This may be due to a blocked port, missing dependencies, improper privileges, a crash, or a shutdown by another method. Press the Logs button to view error logs and check the Windows Event Viewer for more clues If you need more help, copy and post this entire log window on the forums If you have come across either one of errors, mostly issue could be port 80 is used by another service or combination of services or a tool. If you are not a developer, you don’t have development software installed and Skype is installed; first go to  “Tools-> Option -> advanced -> connection” and check port 80 is used there? For developer it’s quite impressive (if the...

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...