[c#]在 asp.net 中更改 img src

标签: Asp.net .Net HTML C#
发布时间: 2017/2/24 23:22:06
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

所以我说是否访客"Inhouse"不是是我的数据库中有一个 bool 列。如果值是 true 我想 img src 是︰ const string checkedIn = "/Images/Icons/Visitor-checkedin-16x16.png"; 如果 false ,我想要另一个 src。此外,如果访客访问了不还,"点播"应该是 false 和"expectedArrival"日期尚未。这里是我的代码︰

C# 中︰

public string GetImageUrl(string inhouse, DateTime expectedArrival)
{
    const string checkedIn = "/Images/Icons/Visitor-checkedin-16x16.png";
    const string checkedOut = "/Images/Icons/Visitor-checkedout-16x16.png";
    const string notArrived = "/Images/Icons/Visitor-notarrived-16x16.png";

    if (ToBool(inhouse))
    {
        inhouse = checkedIn;
    }

    else if (ToBool(inhouse) == false && expectedArrival.AddDays(0) <= DateTime.Now)
    {
        inhouse = notArrived;
    }

    else
    {
        inhouse = checkedOut;
    }

    return inhouse;
}

private static bool ToBool(string value, bool defaultValue = false)
{
    bool result;
    return bool.TryParse(value, out result)
        ? result
        : defaultValue;
}

ASPX:

<asp:Repeater runat="server" OnItemDataBound="rptVisitedItem_OnItemDataBound" OnItemCommand="rptVisitedItem_OnItemCommand" ID="rptVisitedItem">
     <ItemTemplate>
        <tr>
            <td>
                <img src='<%# GetImageUrl(Eval("Inhouse").ToString(), Convert.ToDateTime(Eval("ExpectedArrival"))) %>'alt="" class="statusIcon" />
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

在哪里做错了?我只拿了一个图像显示 atm。

解决方法 1:

认为你可能想做这样的事情从快速审视您的代码︰

public string GetImageUrl(string inhouse, DateTime expectedArrival)
{
    const string checkedIn = "/Images/Icons/Visitor-checkedin-16x16.png";
    const string checkedOut = "/Images/Icons/Visitor-checkedout-16x16.png";
    const string notArrived = "/Images/Icons/Visitor-notarrived-16x16.png";

    bool result;
    bool.TryParse(inhouse, out result);

    if (result)
    {
        inhouse = checkedIn;
    }

    else if (expectedArrival.AddDays(0) <= DateTime.Now)
    {
        inhouse = notArrived;
    }
    else
    {
        inhouse = checkedOut;
    }

    return inhouse;
}
官方微信
官方QQ群
31647020