Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render DateTime.Now directly in the ASPX page

Tags:

asp.net

I am trying to do the following directly into the aspx page but it is not showing the date value. I dont want to do it from the code behind. Am i missing something small here? Pls suggest.

<asp:Literal ID="ltrDate" Text='<% DateTime.Now.ToLongTimeString() %>' runat="server"></asp:Literal>

Even the use of hash in the expression <%#DateTime.Now.ToLongTimeString() %> does not work.

like image 847
Karan Avatar asked Jan 16 '12 07:01

Karan


Video Answer


2 Answers

If you do not need to access the value of the Literal control from the code-behind, then there is no need to use it. Instead, you can just use the following expression in your page directly where you want to print the date:

<%= DateTime.Now.ToLongTimeString() %>

With server controls, you can only put either static text, databinding expressions <%# xx %>, or expression builders <%$ %> inside the property values in the page markup.

See this related question for more details about each approach.

like image 52
patmortech Avatar answered Oct 14 '22 16:10

patmortech


When using a databinding expression such as <%# DateTime.Now.ToLongTimeString() %>, then you have to call Page.DataBind() (or ltrDate.DataBind() if that's the only databound control) from your code-behind (e.g. in Page_Load).

like image 24
M4N Avatar answered Oct 14 '22 16:10

M4N