我有一个asp:GridView,在一列我有
<asp:TemplateField AccessibleHeaderText="Created" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:LinkButton ID="lnkTimeline" runat="server" CausesValidation="False" CommandName="Continue" CommandArgument='<%# Container.DataItemIndex %>' Text='<%# CreatedAt_Proxy %>'></asp:LinkButton>
</ItemTemplate>
和代理:
public string CreatedAt_Proxy
{
get
{
string rv = "";
int secondsPast = (int)DateTime.UtcNow.Subtract(CreatedAt).TotalSeconds;
int threshold = 5 * 24 * 60 * 60;
if (secondsPast >= threshold) rv = "<span style='color: red'>";
rv += E.HourFormat(secondsPast, false) + " ago";
rv += "<br />" + CreatedAt.ToString("dd.MM.yyyy HH:mm");
if (secondsPast >= threshold) rv += "</span>";
return rv;
}
}
我想在 5 月 gridview ( Text='<%# CreatedAt_Proxy %>'
( 中使用该代理,以便我可以生成包含不同时间的文本的链接。
你挂
接到 RowDataBound 事件以根据需要呈现数据。
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnkTimelineButton = e.Row.FindControl("lnkTimeline") as LinkButton ;
lnkTimelineButton.Text= GetProxyTime();
}
创建以下函数并获取代理时间的值。
private string GetProxyTime()
{
string rv = "";
int secondsPast = (int)DateTime.UtcNow.Subtract(CreatedAt).TotalSeconds;
int threshold = 5 * 24 * 60 * 60;
if (secondsPast >= threshold) rv = "<span style='color: red'>";
rv += E.HourFormat(secondsPast, false) + " ago";
rv += "<br />" + CreatedAt.ToString("dd.MM.yyyy HH:mm");
if (secondsPast >= threshold) rv += "</span>";
return rv;
}