Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert an image based on a datafield value 1

Status
Not open for further replies.

kiwieur

Technical User
Apr 25, 2006
200
GB
Hi Guys,

I am not sure if I am posting this in the correct forum but here goes.

I am really new to ASP.NET and I am trying very slowly to convert some existing ASP classic pages to .NET.

In a particular asp page I check for a value in a particular recordset field and then replace it with an image,shown below is the code i am using.

Code:
          <%if (rsLateLoads.Fields.Item("StatusLight").Value)= "R" then%>
            <img src="../Images/RedXSmall.png" width="10" height="10" />
            <%elseif (rsLateLoads.Fields.Item("StatusLight").Value)= "G" then%>
            <img src="../Images/GreenXSmall.png" width="10" height="10" />
        <%elseif (rsLateLoads.Fields.Item("StatusLight").Value)= "ND" then%>
			<img src="../Images/BlueXSmall.png" width="10" height="10" />
          <%else%>
            <img src="../Images/AmberXSmall.png" width="10" height="10" />
            <%end if%>

Is it possible to create something similar to this in asp.net, I have managed to create a gridview to display the data and this is what I have at the moment

Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="float:left" 
        DataSourceID="DespLoads"
        DataKeyNames="LoadID"
        GridLines="None"
        CssClass="mGrid"
        AlternatingRowStyle-CssClass="alt" Width="550px">
        <Columns>
            <asp:CommandField HeaderText="Details" SelectText="Go" 
                ShowSelectButton="True" />
            <asp:BoundField DataField="LoadID" HeaderText="LoadID" 
                SortExpression="LoadID" />
            <asp:BoundField DataField="StatusDesc" HeaderText="Status" 
                SortExpression="StatusDesc" />
            <asp:BoundField DataField="PlanShipDate" HeaderText="Plan ShipDate/Time" 
                SortExpression="PlanShipDate" />
            <asp:BoundField DataField="StatusDateTime" HeaderText="Status Date/Time" 
                SortExpression="StatusDateTime" />
            <asp:BoundField DataField="StatusLight" HeaderText="Flag" 
                SortExpression="StatusLight" />
        </Columns>
        <AlternatingRowStyle CssClass="alt" />
    </asp:GridView>

Any help or suggestions would be most appreciated.

[ponder][ponder]

Regards

Paul

 
OK, rather than using a BoundField, use a Template field with an asp:Image control in it. Then, in the RowDataBound event of the GridView, you can do something like:
Code:
Dim i as Image = e.Row.FindControl("myImage")
Select Case e.Row.DataItem("StatusLight")
  Case "R"
   i.ImageURL = "../Images/RedXSmall.png"
  ... etc
End Select

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Hi Mark,

Thanks for your speedy response, sorry to be a pain but could you expand a little on how I would use the template field and image control please. As I said I am trying to get to grips with .net but am struggling with this at the moment

Regards

Paul

 
Hi Mark,

sorry I have taken a while to get back to you but I actually managed to solve my issue by using the following

Code:
            <asp:ImageField DataImageUrlField="StatusLight" DataImageUrlFormatString="~/Images/{0}.png" HeaderText="Status"/>

the status light values in teh DB table were as follows R,A,B so I called my images by those names then by using that code it displayed the correct image.

I am in the process of looking at the template fields as you suggested.

thanks for taking the time to reply

Regards

Paul

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top