469,269 Members | 1,006 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,269 developers. It's quick & easy.

set ASP.NET datagrid image based on SQL table value

I have an ASP.NET datagrid that loads an image. I would like to load
one image if the value in my SQL table is true and another if it is
false. Here is my code.

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="Search.aspx.vb" Inherits="PROJECT.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="VBScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="MyGrid" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px" runat="server"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
BackColor="#DEBA84" CellPadding="3"
AutoGenerateColumns="False" CellSpacing="2">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<Columns>
</asp:BoundColumn>
<asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Check or X">
<ItemTemplate>
<img src='\Check.gif'>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML>

I want to show the 'Check.gif' if an SQL table value is True or
'x.gif' if it is false.

Thaks for any help, This ASP.NET is kicking my butt.
Nov 18 '05 #1
3 2178
Hi Rob,

Just jam a little

<IMG src='<%#iif(DataBinder.Eval(Container,
"DataItem.Boolean"),"Check.gif","x.gif") %>'>

in there and you should be okay.

BTW, I had some problems with your markup. My fixed version is below.

Does this help?

Ken
Microsoft MVP [ASP.NET]
Toronto
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="VBScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="MyGrid" runat="server" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px"
BackColor="#DEBA84" CellPadding="3" AutoGenerateColumns="False"
CellSpacing="2">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Check or X">
<ItemTemplate>
<IMG src='<%#iif(DataBinder.Eval(Container,
"DataItem.Boolean"),"Check.gif","x.gif") %>'>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML>

"Rob Rogers" <ro*@natltc.com> wrote in message
news:7e**************************@posting.google.c om...
I have an ASP.NET datagrid that loads an image. I would like to load
one image if the value in my SQL table is true and another if it is
false. Here is my code.

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="Search.aspx.vb" Inherits="PROJECT.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="VBScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="MyGrid" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px" runat="server"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
BackColor="#DEBA84" CellPadding="3"
AutoGenerateColumns="False" CellSpacing="2">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<Columns>
</asp:BoundColumn>
<asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Check or X">
<ItemTemplate>
<img src='\Check.gif'>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML>

I want to show the 'Check.gif' if an SQL table value is True or
'x.gif' if it is false.

Thaks for any help, This ASP.NET is kicking my butt.


Nov 18 '05 #2
BTW, here's the code behind and data source that I used for that example.
"Boolean" is the name of the field as well as the type.

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
MyGrid.DataSource = CreateDataSource()
MyGrid.DataBind()

End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("City", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

"Rob Rogers" <ro*@natltc.com> wrote in message
news:7e**************************@posting.google.c om...
I have an ASP.NET datagrid that loads an image. I would like to load
one image if the value in my SQL table is true and another if it is
false. Here is my code.

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="Search.aspx.vb" Inherits="PROJECT.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="VBScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="MyGrid" style="Z-INDEX: 101; LEFT: 0px; POSITION:
absolute; TOP: 0px" runat="server"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
BackColor="#DEBA84" CellPadding="3"
AutoGenerateColumns="False" CellSpacing="2">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<Columns>
</asp:BoundColumn>
<asp:BoundColumn DataField="City" ReadOnly="True" HeaderText="City">
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Check or X">
<ItemTemplate>
<img src='\Check.gif'>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510"
Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML>

I want to show the 'Check.gif' if an SQL table value is True or
'x.gif' if it is false.

Thaks for any help, This ASP.NET is kicking my butt.


Nov 18 '05 #3
Thanks, Thats just what I needed.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jeff | last post: by
4 posts views Thread by Trevor Hartman | last post: by
14 posts views Thread by Brett Sinclair | last post: by
reply views Thread by rcoco | last post: by
3 posts views Thread by boyindie86 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.