2011-10-01 15 views
31

我想弄清楚如何改变日期时间格式,所以日期会显示出来。如何在asp.net中更改DataBinder.Eval的日期格式?

  <asp:Repeater ID="RepeaterActions" runat="server"> 
      <ItemTemplate> 
       <li> 
        <span class="historyDate"><%#DataBinder.Eval(Container.DataItem, "ActionListDate")%></span> 
        <span class="historyName"><%#DataBinder.Eval(Container.DataItem, "LeadActionName")%></span><br /> 
        <span class="historyNotes"><%#DataBinder.Eval(Container.DataItem, "ActionListNote")%></span> 
       </li> 
      </ItemTemplate> 
     </asp:Repeater> 

我猜这件事情在<%%>之间,但我不知道。

我后面的代码是:

<pre> 
     protected void RepeaterActionsFill() 
    { 

     string sql = @" select a.ActionListDate, a.LeadListID, 
a.ActionListNote, 
l.LeadActionName 
from ActionLists as a 
INNER JOIN LeadActions as l 
ON a.LeadActionID = l.LeadActionID 
where a.LeadListID = " + Convert.ToInt32(Request["id"].ToString()); 

     RepeaterActions.DataSource = DBUtil.FillDataReader(sql); 
     RepeaterActions.DataBind(); 
    } 
</pre> 

目前,涉及翻过看起来像这样:

HTML View

什么我要找的是时间戳并在那里走了。

任何帮助表示赞赏。

编辑:

这里就是我一直在寻找:

  <asp:Repeater ID="RepeaterActions" runat="server"> 
      <ItemTemplate> 
       <li> 
        <span class="historyDate"><%#DataBinder.Eval(Container.DataItem, "ActionListDate", "{0:M/d/yy}")%></span> 
        <span class="historyName"><%#DataBinder.Eval(Container.DataItem, "LeadActionName")%></span><br /> 
        <span class="historyNotes"><%#DataBinder.Eval(Container.DataItem, "ActionListNote")%></span> 
       </li> 
      </ItemTemplate> 
     </asp:Repeater> 

回答

44

给格式如:

<%# DataBinder.Eval(Container.DataItem, "ActionListDate", "{0:d/M/yyyy hh:mm:ss tt}") %> 
-3

现在不能测试此代码,但沿线的东西吗?

<%#DataBinder.Eval(Container.DataItem, "ActionListDate").ToString("dd/MM/yyyy") %> 
+0

这不起作用,因为.ToString()不能接受任何参数。 – banging

23

<%# string.Format("{0:ddd MMM yyyy}", Eval("ActionListDate"))%>

0

你也可以将您的TSQL更改为以下或只是删除Convert.ToInt32()

string sql = string.Format(@"select a.ActionListDate, a.LeadListID, 
          a.ActionListNote, 
          l.LeadActionName 
          from ActionLists as a 
          INNER JOIN LeadActions as l 
          ON a.LeadActionID = l.LeadActionID 
          where a.LeadListID = {0};", Request["id"]); 
+0

您还应该检查任何SQL注入的Request [“id”] ... – Christian

1

我把这个在后面的代码:

public string makeShortDate(object oDate) 
{ 
    if (oDate is DBNull) { 
     return ""; 
    } else { 
     DateTime dDate = Convert.ToDateTime(oDate); 
     string sDate = dDate.ToShortDateString(); 
     return sDate; 
    }   
} 

而在XHTML使用: 文本= '<%#makeShortDate(DataBinder.Eval的(的Container.DataItem, “指明MyDate”))%>' 您可以修改此为任何类型。