HelperFunction\Image

G

gh

I am using VS 2008. There datalist hase a filed, STATUS, that is 0, 1,
or 2. Depending on the value I want to set the Image source. I also
have a field with a description that I would like to concantenate to the
image, as the hyperlink or use a hyperlink filed for everything Below
is what I have for a function in the code behind:

protected string SetStatus(string aStatus)
{
if (aStatus == "0")
{
return "<img src = \"../images/image1.gif\" />";
}
else
if (aStatus == "0")
{
return "<img src = \"../images/image2.gif\" />";
}

}

In the aspx file below is the template markup.

<asp:DataList ID="DataList1" runat="server" RepeatColumns="3"
RepeatDirection="Horizontal" DataSourceID="SqlDataSource1"
onitemdatabound="DataList1_ItemDataBound">
<ItemTemplate>
<br />
STATUS:
<asp:Label ID="STATUSLabel" runat="server" Text='
<%# SetStstus(Convert.ToString(DataBinder.Eval(Container.DataItem,
"STATUS")))%>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>


1. Can I add the markup to use an image and hyperlink for the cell?
2. When I try to view the page now I get the following error:

Compiler Error Message: CS0161: '_Default.SetStatus(string)': not
all code paths return a value


Line 98: }
Line 99:
Line 100: protected string SetStatus(string aStatus)
Line 101: {
Line 102: /* if (aStatus == "0")

The Status is stored as and integer but I am using a Convert.ToString in
the SetStatus call.

Waht would be the cause of the error?

TIA
 
M

Milosz Skalecki [MCAD]

Howdy,

I didn't quite get what you wanted to do with hyperlink and description, but
please find the resolution below (it's just a guess, if you could be more
specific please). Your compiler error is caused by the SetStatus function, as
it does not always return a value, i.e.
string GetStatusImage(string status)
{
if (status == "0")
return "~/img/Pending.gif"
else if (status == "1")
return "~/img/Processing.gif";
else if (status == "2")
return "~/img/Complete.gif";
}
as you can see, function takes care of just two values "0" and "1", and if
you passed any other string (i.e. "2" or even "longtext") the result would be
unknown, which is not acceptable. Therefore in order to take care of all
other statuses, it needs to be changed to:
string GetStatusImage(string status)
{
if (result == "0")
return "~/img/pending.gif"
else if (result == "1")
return "~/img/processing.gif";
else
return "~/img/complete.gif";
}
In this particular case you have only three possible integere values, so it
would be better to define enumeration:

public enum TaskStatus
{
Pending = 0,
Processing = 1,
Complete = 2
}

<asp:DataList runat="server" ID="list" RepeatLayout="Table"
RepeatDirection="Vertical">
<ItemTemplate>
<asp:HyperLink runat="server" ID="link" BorderWidth="0"
ToolTip='<%# Eval("Description") %>'
NavigateUrl="#" ImageUrl='<%#
GetStatusImage((TaskStatus)Eval("Status")) %>' />
</ItemTemplate>
</asp:DataList>

protected string GetStatusImage(TaskStatus status)
{
switch (status)
{
case TaskStatus.Pending: return "~/img/pending.gif";
case TaskStatus.Processing: return "~/img/processing.gif";
default: return "~/img/complete.gif"; // covers all other
enumeration values
}
}

Hope this helps.
 
G

gh

Milosz said:
Howdy,

I didn't quite get what you wanted to do with hyperlink and description, but
please find the resolution below (it's just a guess, if you could be more
specific please). Your compiler error is caused by the SetStatus function, as
it does not always return a value, i.e.
string GetStatusImage(string status)
{
if (status == "0")
return "~/img/Pending.gif"
else if (status == "1")
return "~/img/Processing.gif";
else if (status == "2")
return "~/img/Complete.gif";
}
as you can see, function takes care of just two values "0" and "1", and if
you passed any other string (i.e. "2" or even "longtext") the result would be
unknown, which is not acceptable. Therefore in order to take care of all
other statuses, it needs to be changed to:
string GetStatusImage(string status)
{
if (result == "0")
return "~/img/pending.gif"
else if (result == "1")
return "~/img/processing.gif";
else
return "~/img/complete.gif";
}
In this particular case you have only three possible integere values, so it
would be better to define enumeration:

public enum TaskStatus
{
Pending = 0,
Processing = 1,
Complete = 2
}

<asp:DataList runat="server" ID="list" RepeatLayout="Table"
RepeatDirection="Vertical">
<ItemTemplate>
<asp:HyperLink runat="server" ID="link" BorderWidth="0"
ToolTip='<%# Eval("Description") %>'
NavigateUrl="#" ImageUrl='<%#
GetStatusImage((TaskStatus)Eval("Status")) %>' />
</ItemTemplate>
</asp:DataList>

protected string GetStatusImage(TaskStatus status)
{
switch (status)
{
case TaskStatus.Pending: return "~/img/pending.gif";
case TaskStatus.Processing: return "~/img/processing.gif";
default: return "~/img/complete.gif"; // covers all other
enumeration values
}
}

Hope this helps.


Milosz:

I have a query that returns a Status, CountryID, and CountryName. I am
dynamically creating a datalist of links for each country. The link
will have a CountryName and the Href( listpage.aspx?ID=2">. The
link(href) is a combination of listpage.aspx?ID=CountryID and the link
would get the text to display from CountryName.
What I am not able to work out is getting the Image with the CountryName
and link setup as one. I have images with the link, but the country
names are not being displayed. Should a hyperlink work for all these?

TIA
 
M

Milosz Skalecki [MCAD]

If I got you right, you will need two separate hyperlinks, one for country
name text and link, second for image and link. Still not sure what you want
to achieve ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top