Format a date in text of LinkButton

  • Thread starter Thread starter David C
  • Start date Start date
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>
 
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.
 
Back
Top