Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HelperFunction\Image

Reply
Thread Tools

HelperFunction\Image

 
 
gh
Guest
Posts: n/a
 
      05-10-2008
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.

<aspataList 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(Contain er.DataItem,
"STATUS")))%>' />
<br />
<br />
</ItemTemplate>
</aspataList>


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
 
Reply With Quote
 
 
 
 
Milosz Skalecki [MCAD]
Guest
Posts: n/a
 
      05-10-2008
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
}

<aspataList 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>
</aspataList>

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


"gh" wrote:

> 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.
>
> <aspataList 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(Contain er.DataItem,
> "STATUS")))%>' />
> <br />
> <br />
> </ItemTemplate>
> </aspataList>
>
>
> 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
>

 
Reply With Quote
 
 
 
 
gh
Guest
Posts: n/a
 
      05-11-2008
Milosz Skalecki [MCAD] wrote:
> 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
> }
>
> <aspataList 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>
> </aspataList>
>
> 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
 
Reply With Quote
 
Milosz Skalecki [MCAD]
Guest
Posts: n/a
 
      05-12-2008
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
--
Milosz


"gh" wrote:

> Milosz Skalecki [MCAD] wrote:
> > 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
> > }
> >
> > <aspataList 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>
> > </aspataList>
> >
> > 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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments