ASP.NET在asp:GridView中使用代理值

时间:2017-03-17 07:27:48

标签: c# html asp.net

我有一个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;
    }
}

我想在may gridview(Text='<%# CreatedAt_Proxy %>')中使用该代理,以便我可以生成包含不同时间的文本的链接。

1 个答案:

答案 0 :(得分:0)

您可以挂钩到RowDataBound事件以根据需要呈现数据。

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        LinkButton lnkTimelineButton = e.Row.FindControl("lnkTimeline") as LinkButton ;
        lnkTimelineButton.Text= GetProxyTime();
    }

//创建以下函数并获取proxytime的值。

    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;
    }
相关问题