5

So I have some code that looks like this:

<asp:BoundField DataField="CreatedOn" HeaderText="Created on" 
SortExpression="CreatedOn" DataFormatString="{0:MMM dd yyyy hh:mm:ss tt}">

This works as I would like it to. However, I want to reuse the date format everywhere in my program. Thus, I want to use a variable instead of the string used above. Something like:

<asp:BoundField DataField="CreatedOn" HeaderText="Created on" 
SortExpression="CreatedOn" DataFormatString="<%=myFormatString%>">

But this totally doesn't work. It prints out literally:

<%=myFormatString%>

I tried Bind, I tried Eval, nothing seems to work. It seems to me this should be really simple, even necessary. Am I the only person in the world who wants to use a DataFormatString more than once? Is this possible or am I a dreamer?

Andrew
  • 1,571
  • 17
  • 31
  • Can you please post your "myFormatString" function/property? – Philipp Apr 06 '12 at 20:59
  • It's just a string: `public static string myFormatString = "{0:MMM dd yyyy}"` I was under the impression if the error was on that side, it would tell me something and not just print out the text literally. – Andrew Apr 09 '12 at 13:40
  • I also tried it as a property with a get simply returning a string. – Andrew Apr 09 '12 at 14:01

3 Answers3

4

You could set it from codebehind:

((BoundField)GridView1.Columns[ColumnIndex]).DataFormatString = myFormatString;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • This does indeed work if I put it in the DataBound method. I'll accept it as the answer if nobody can come up with anything better. It just seems like since I have a constant string, I should be able to plug it in with <%= %>. If the answer is "you simply can't do that because of no apparent reason," then that sucks. – Andrew Apr 09 '12 at 15:41
  • So it seems you simply can't do it because of no apparent reason. I'd like to note I also had to do this on a Telerik.Web.UI.GridBoundColumn and that solution is even different from this one. For that solution I had to do it in the PreRender method for the control and then call the blah.MasterTableView.Rebind() method (see "Declarative Columns" [here](http://www.telerik.com/help/aspnet-ajax/grid-using-columns.html) – Andrew Apr 13 '12 at 16:41
4

One option would be to create a new class, DateBoundField (or whatever you want to name it), which inherits bound field. In the constructor you just set the DataFormatString to your date format string, then use the DateBoundField everywhere for your dates, when you need to change your date format, just change the format string in the constructor of this class.

    public class DateBoundField : BoundField
    {
        public DateBoundField()
        {
             DataFormatString = "{0:yyyy-MMM-dd}";
        }
    }
cbeuker
  • 947
  • 1
  • 11
  • 21
  • 1
    I found this to be a pretty nice way. For those who need it, you'll need to add something like the following near the top of your `.aspx` file: `<%@ Register TagPrefix="x" Namespace="y" Assembly="z" %>`, where `x` is an invented tag to use with the control instead of ` – Craig Silver Nov 28 '18 at 04:06
1

I guess I just found the solution, i never used it before so you need to try :-)

Description url not being decoded asp.net

I would try something like:

<%# myFormatString() %>
Community
  • 1
  • 1
Philipp
  • 1,425
  • 1
  • 11
  • 23
  • 1
    Negative. Still prints out literally. – Andrew Apr 09 '12 at 13:59
  • have you tried the property on a different object? like: I have the guess, that server side validated objects do not accept inline asp code. I already had similar problems in aspx/.net with runat="server". – Philipp Apr 09 '12 at 19:06
  • Yep, that works as expected, printing out the string correctly. I guess this is just some asp.net thing where they don't want things to be easy for me... – Andrew Apr 10 '12 at 13:51