【问题标题】:Change img src in asp.net在 asp.net 中更改 img src
【发布时间】:2016-12-22 08:44:02
【问题描述】:

所以我的数据库中有一个 bool 列,说明访问者是否为“内部”。如果值为true,我希望img src 为:const string checkedIn = "/Images/Icons/Visitor-checkedin-16x16.png"; 如果false,我希望它是另一个src。此外,如果访客尚未访问,“Inhouse”应该是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。

【问题讨论】:

  • 为什么要将 Inhouse 转换为字符串,然后将其转换回布尔值?这是没有意义的,并且可能是错误的来源。其次,您正在滥用内部变量 - 同时用作(布尔)输入和(字符串)输出。使用单独的变量 - 没有理由不这样做,这是一种令人困惑的编码方式。
  • 你能发个帖子用代码告诉我你的意思吗?
  • 你应该调试你的代码(例如console.writeline,....),我想这与显示图像无关,而是你在GetImageUrl中的逻辑

标签: c# html asp.net .net


【解决方案1】:

有几个明显的问题需要解决:

1)假设你的Repeater中原来的“inhouse”变量是一个bool,然后将它转换成一个字符串,然后再将它转换回一个bool是没有意义的,并且因为你的ToBool方法不正确而导致了问题。

如果转换过程成功,TryParse 返回true,否则返回false(请务必阅读手册!https://msdn.microsoft.com/en-us/library/system.boolean.tryparse(v=vs.110).aspx)。 转换后的 bool 的实际值 包含在 result 中,您不会从方法中返回它。相反,您正在返回成功值。如果您的转换始终成功,则 ToBool 始终返回 true,这就是您始终获得相同图像的原因。

2) 你通过赋予它两个含义来滥用 inhouse 变量 - 首先它是一个布尔值来表示是否有东西在内部,然后它是一个包含图像 URL 的字符串。不应该两者兼而有之。一个变量应该有一个单一的目的。你所做的是不好的做法,并且会混淆阅读它的任何其他人(包括你,在一年的时间内!)。这样做也会造成意想不到的问题。

所以,在中继器中:

<%# GetImageUrl(Eval("Inhouse"), Convert.ToDateTime(Eval("ExpectedArrival"))) %>

(另外,还有一点:如果 ExpectedArrival 还不是 DateTime,为什么不呢?它应该是。如果它来自您的数据库,它不应该是字符串。如果它 是 已经是DateTime,无需转换)。

二、方法:

public string GetImageUrl(bool 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";
    string imageUrl = null; //separate variable to hold the chosen image URL

    if (inhouse == true)
    {
        imageUrl = checkedIn;
    }
    else if (inhouse == false && expectedArrival.AddDays(0) <= DateTime.Now)
    {
        imageUrl = notArrived;
    }
    else
    {
        imageUrl = checkedOut;
    }

    return imageUrl;
}

【讨论】:

  • Inhouse 错误:“参数 1:无法从 'object' 转换为 'bool ' 参数类型 'object' 不能分配给参数类型 'bool'
  • 所以你是说在这种情况下“内部”是object?为什么它在您的班级中不是布尔值?如果无法更改,您始终可以将其转换为布尔值:Convert.ToBoolean(Eval("Inhouse"))
【解决方案2】:

认为您可能正在尝试通过快速查看您的代码来做这样的事情:

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;
}

【讨论】:

  • 值得添加的布尔值默认总是false。
猜你喜欢
  • 2011-11-26
  • 2016-04-17
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 2018-09-15
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多