Format a date in text of LinkButton

D

David C

I am trying to get a LinkButton to show a formatted date as part of the text
but it is not working. Below is what I have. MealDate is a valid date, e.g.
11/17/2008. I want it to come out like "Novenber 17". Below is my button
settings.
Thanks.
David

<asp:LinkButton ID="LinkButton1" runat="server"
Text='Sun<br /><%# String.Format(Eval("MealDate","{0:MMMM dd}") %>'
Width="110" CommandArgument='<%# Eval("MealDate", "{0:d}") %>'
CommandName="Insert" OnCommand="LBtnMealEdit_Command">
</asp:LinkButton>
 
N

Nathan Sokalski

I am assuming that your LinkButton is inside of a Template in a Control such
as a Repeater or DataList. When formatting the value of a field when using
the Eval method, you do not need the String.Format. Try the following:

<asp:LinkButton ID="LinkButton1" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"MealDate","{0:MMMM dd}") %>' Width="110"
CommandArgument='<%# DataBinder.Eval(Container.DataItem,"MealDate", "{0:d}")
%>' CommandName="Insert" OnCommand="LBtnMealEdit_Command"/>

I also noticed that the value for your Text property had a constant and a
databound value. It is usually better to do this using the formatstring
parameter of the DataBinder.Eval method, such as the following:

Text='<%# DataBinder.Eval(Container.DataItem,"MealDate","Sun<br/>{0:MMMM
dd}") %>'

Hopefully this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top