简体   繁体   中英

Change img src in asp.net

So I have a bool column in my database that says if visitor is "Inhouse" or not. If the value is true I want the img src to be: const string checkedIn = "/Images/Icons/Visitor-checkedin-16x16.png"; if false , I want it to be another src. And also, if visitor has not visited yet, "Inhouse" should be false AND the "expectedArrival" date has not been yet. Here is my code:

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>

Where have I done wrong? I only get one image to show atm.

There are a couple of obvious problems which need clearing up:

1) Assuming the original "inhouse" variable in your Repeater is a bool, then converting it to a string, and then converting it back to a bool is pointless, and has caused a problem because your ToBool method is incorrect.

TryParse returns true if the conversion process succeeded and false if it did not (always read the manual! https://msdn.microsoft.com/en-us/library/system.boolean.tryparse(v=vs.110).aspx ). The actual value of the converted bool is contained in result , which you are not returning from the method. Instead you are returning the success value. If your conversion always succeeds, then ToBool always returns true, so that's why you always get the same image.

2) You're abusing the inhouse variable by giving it two meanings - first it's a bool to say whether something's in house or not, then later it's a string containing the image URL. It shouldn't be both. A variable should have a single purpose. What you've done is bad practise and will confuse anyone else who reads it (including you, in a year's time!). Doing things like that is also a way to create unexpected problems.

So, in the repeater:

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

(Also, another point: If ExpectedArrival is not already a DateTime, why not? It ought to be. If it's coming from your database it should not be a string. If it is already a DateTime, there's no need to convert it).

Secondly, the method:

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

Think you're probably trying to do something like this from taking a quick look at your code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM