HelperFunction\Image

Discussion in 'ASP .Net' started by gh, May 10, 2008.

  1. gh

    gh Guest

    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
     
    gh, May 10, 2008
    #1
    1. Advertising

  2. 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


    "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.
    >
    > <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
    >
     
    Milosz Skalecki [MCAD], May 10, 2008
    #2
    1. Advertising

  3. gh

    gh Guest

    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
    > }
    >
    > <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
     
    gh, May 11, 2008
    #3
  4. 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
    > > }
    > >
    > > <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
    >
     
    Milosz Skalecki [MCAD], May 12, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry J
    Replies:
    0
    Views:
    572
    Jerry J
    Jul 18, 2003
  2. Mike John
    Replies:
    0
    Views:
    540
    Mike John
    Jul 28, 2003
  3. Gary B

    Image can't hold an image?

    Gary B, Jul 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    366
    Steve C. Orr, MCSD
    Aug 1, 2003
  4. Jim Hammond
    Replies:
    1
    Views:
    498
    Oliver
    Nov 13, 2003
  5. Laszlo Zsolt Nagy
    Replies:
    1
    Views:
    1,437
    Kartic
    Jan 26, 2005
Loading...

Share This Page