How do I format a date pulled from a database? - asp.net

I am trying to pull a date from database and putting it on a webpage with the below code:
<asp:Label ID="Label3" runat="server" Text='<%# Eval("TravelDate") %>' /><br /><br />
It pulls the date with no problem, but when it shows up on the page, no matter how it is formatted in the database, it seems to want to always display the date as "6/17/2013 12:00:00am". Is there something I'm missing in the VS portion that I have to use to format the date? I'd prefer "June 17, 2013", but the only option close to that in Access is where it adds the day of the week in front of it. Getting rid of the time is important.

<asp:Label ID="txtDate" Width="65px" runat="server" Font-Size="8.5pt" ForeColor="#000f9f"
Text='<%# Eval("How_date","{0:dd/MMM/yyyy}") %>'></asp:Label>

Try to extract like the field like this:
Expr1:Format([Field Name],"DD/MM/YYYY")
or
If it's a text field then you can use the string function left() or right() to get the date. Expr1:Left([Field Name],10)

Just remove the single quotation mark '' after the property text of the TextBox.
<asp:Label ID="Label3" runat="server" Text=<%# Eval("TravelDate", "{0:MMMM dd, yyyy}") %> /><br /><br />

Related

how to get date from ajax calender in asp.net

The following is my .aspx code for ajax calender
<ajax:CalendarExtender ID="CalendarExtender1" TargetControlID="TextBox1" runat="server">
</ajax:CalendarExtender>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td><td>
and aspx.cs code is
string date = Request.Form[TextBox1.UniqueID];
comp.DATETIME = Convert.ToDateTime(date);
string value could not converted to datetime. how to convert this into datetime
<td>
Date Received From
</td>
<td >
<asp:TextBox ID="TxtSearchDate_Received_From" runat="server"></asp:TextBox>
<a runat="server" id="HrefDateReceivedFrom"></a>
<asp:CalendarExtender Format="dd-MM-yyyy" ID="CalendarExtender1" runat="server" TargetControlID="TxtSearchDate_Received_From"
PopupButtonID="HrefDateReceivedFrom" Enabled="True" />
</td>
Then use txtsearchDate_received_from control on the server side. This is a good practice as the use can view selected date in text box control.
Let me know if it works for you
Why haven't you tried just getting the .Text property value from the server control, like this?
string date = this.TextBox1.Text;
Then you can do your conversion to DateTime, like this:
comp.DATETIME = Convert.ToDateTime(date);

ajax calender extender month name display

i have added an extender to the text box to select date. The date it shows the number of the corresponing month. is there any way to get the month name instead of the month number? for eg i get as 04-04-13 i want it as 04-Apr-13. Thanks in advance
the code i have is
<asp:TextBox ID="txtInvDate" runat="server" Height="21px" Width="88px"
ontextchanged="txtInvDate_TextChanged" AutoPostBack="True"></asp:TextBox>
<asp:CalendarExtender ID="txtInvDate_CalendarExtender"
runat="server" TargetControlID="txtInvDate" Format="dd-MM-yyyy">
</asp:CalendarExtender>
You could try following format:
<asp:CalendarExtender ID="txtInvDate_CalendarExtender" runat="server" TargetControlID="txtInvDate"
Format="dd-MMM-yyyy" >
</asp:CalendarExtender>
Custom Date and Time Format Strings
particularly: The "MMM" Custom Format Specifier
Note that it depends on the current culture on the server.

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>
This works:
<asp:Label id="dada" runat="server" Text='<%# string.Format("{0}",string.IsNullOrEmpty(Eval("CineRunFrom").ToString())?"N/A":Eval("CineRunFrom")) %>' ></asp:Label>
You may use this: Text='<%# Eval("CineRunFrom")?? "N/A" %>'
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>

asp:DataList control with asp:LinkButton inside - something's weird

I'm working through examples in a book trying to learn ASP.NET, and I've stumbled on something strange in there. First of all, if I type it as it's written in the book, VS gives me errors. This is the code as it's written in the book:
<asp:DataList ID="employeesList" runat="server">
<ItemTemplate>
<asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" />
Name: <strong><%#Eval("Name") %></strong><br />
Username: <strong><%#Eval("Username") %></strong><br />
<asp:LinkButton ID="detailsButton" runat="server" Text=<%#"View more details about " + Eval("Name")%>
CommandName="MoreDetailsPlease"
CommandArgument=<%#Eval("EmployeeID")%> />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
So, I've plucked at it for a while, and came up with this solution which actually compiles:
<asp:DataList ID="employeesList" runat="server" onitemcommand="employeesList_ItemCommand">
<ItemTemplate>
<asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" />
Name: <strong><%#Eval("Name") %></strong><br />
Username: <strong><%#Eval("Username") %></strong><br />
<asp:LinkButton ID="detailsButton" runat="server" Text='View more details about <%# Eval("Name") %>'
CommandName="MoreDetailsPlease" CommandArgument='<%Eval("EmployeeID") %>' />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
Notice that I've also added the OnItemCommand in the asp:DataList tag, so now I'm able to fire the event as expected.
However, results in the browser isn't what I expect; the Name and Username listed in strong text show just fine, but the Literal control that should show extra details (the EmployeeID field) and the Name field inside the LinkButton won't show their values in the page:
not showing as expected http://lh6.ggpht.com/_x84bQLYH57A/SgxzygartcI/AAAAAAAAAIY/nhT-6RUJa6o/s144/EmployeeDirectory_notshowing.jpg
It should say "EmployeeID: 1" and "View more details about Zak Ruvalcaba"
I guess it's the Eval function that's not working when inside another control, can anyone help me out?
Change the LinkButton as :
<asp:LinkButton ID="detailsButton" runat="server"
Text='<%# Eval("Name", "View more details about {0}") %>'
CommandName="MoreDetailsPlease"
CommandArgument='<%# Eval("EmployeeID") %>' />
Sorry I confused the order of parameters. I updated my answer. Format must be the second parameter.
You can view another question I posted yesterday concerning something eerily similar here:
Need help with Eval inside DataList
I do believe Canavar actually gave the correct answer, however.

Formatting DataBinder.Eval data

How can I format data coming from a DataBinder.Eval statement in an ASPX page?
For example, I want to display the published date of the news items in a particular format in the homepage. I'm using the ASP.NET 2.0 Repeater control to show the list of news items.
The code for this goes like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<HeaderTemplate><table cellpadding="0" cellspacing="0" width="255"></HeaderTemplate>
<ItemTemplate>
<tr><td >
<a href='/content/latestNews.aspx?id=<%#DataBinder.Eval(Container.DataItem, "id") %>'>
<asp:Label ID="lblNewsTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'></asp:Label>
</a>
</td></tr>
<tr><td>
<asp:Label ID="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate"))%>'></asp:Label>
</td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate></asp:Repeater>
Is there a way I could call a custom method with the DataBinder.Eval value as its parameter (something like below)?
<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>
If yes, then where do I write the GetDateInHomepageFormat method? I tried out in the code behind page but got a run time error?
If this is not possible, is there a way to do inline formatting?
There is an optional overload for DataBinder.Eval to supply formatting:
<%# DataBinder.Eval(Container.DataItem, "expression"[, "format"]) %>
The format parameter is a String value, using the value placeholder replacement syntax (called composite formatting) like this:
<asp:Label id="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate", "{0:dddd d MMMM}") %>'</label>
After some searching on the Internet I found that it is in fact very much possible to call a custom method passing the DataBinder.Eval value.
The custom method can be written in the code behind file, but has to be declared public or protected. In my question above, I had mentioned that I tried to write the custom method in the code behind but was getting a run time error. The reason for this was that I had declared the method to be private.
So, in summary the following is a good way to use DataBinder.Eval value to get your desired output:
default.aspx
<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>
default.aspx.cs code:
public partial class _Default : System.Web.UI.Page
{
protected string GetDateInHomepageFormat(DateTime d)
{
string retValue = "";
// Do all processing required and return value
return retValue;
}
}
Hope this helps others as well.
Why not use the simpler syntax?
<asp:Label id="lblNewsDate" runat="server" Text='<%# Eval("publishedDate", "{0:dddd d MMMM}") %>'</label>
This is the template control "Eval" that takes in the expression and the string format:
protected internal string Eval(
string expression,
string format
)
http://msdn.microsoft.com/en-us/library/3d2sz789.aspx
You can use a function into a repeater like you said, but notice that the DataBinder.Eval returns an object and you have to cast it to a DateTime.
You also can format your field inline:
<%# ((DateTime)DataBinder.Eval(Container.DataItem,"publishedDate")).ToString("yyyy-MMM-dd") %>
If you use ASP.NET 2.0 or newer you can write this as below:
<%# ((DateTime)Eval("publishedDate")).ToString("yyyy-MMM-dd") %>
Another option is to bind the value to label at OnItemDataBound event.
This line solved my problem:
<%#DateTime.Parse(Eval("DDDate").ToString()).ToString("dd-MM-yyyy")%>
To format the date using the local date format use:
<%#((DateTime)Eval("ExpDate")).ToString("d")%>
How to Format an Eval Statement to Display a Date using Date Locale
Thanks to all. I had been stuck on standard format strings for some time. I also used a custom function in VB.
Mark Up:-
<asp:Label ID="Label3" runat="server" text='<%# Formatlabel(DataBinder.Eval(Container.DataItem, "psWages1D")) %>'/>
Code behind:-
Public Function fLabel(ByVal tval) As String
fLabel = tval.ToString("#,##0.00%;(#,##0.00%);Zero")
End Function
Text='<%# DateTime.Parse(Eval("LastLoginDate").ToString()).ToString("MM/dd/yyyy hh:mm tt") %>'
This works for the format as you want
<asp:Label ID="ServiceBeginDate" runat="server" Text='<%# (DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:yyyy}") == "0001") ? "" : DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:MM/dd/yyyy}") %>'>
</asp:Label>
You can use it this way in aspx page
<%# DataBinder.Eval(Container.DataItem, "DateColoumnName", "{0:dd-MMM-yyyy}") %>

Resources