Conditional True/False output for datalist item

G

googlegroups

Hi All.

I have a datalist which pulls back a list of categories from a
database. In the returned table are the CategoryID, the CategoryName
and the IsActive Field.

CategoryID is an integer, CategoryName is a VarChar(250) and IsActive
is bit.

When the data comes back and I try to display it on the webpage,
everything works but rows with an IsActive field set to 1 (true) are
displayed as "1", and IsActive fields set to 0 are displayed as "0"
(false)

I would like to know how to change this to "Active" if a 1 is returned
or "Draft" if 0 is returned.

Thanks in advance
 
K

Ken Cox [Microsoft MVP]

Hi,

I'd use a little helper function for that. Once you get the bit (boolean)
value, the helper function can turn it into a string that fits your need.
See the code below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
If Not IsPostBack Then
DataList1.DataSource = CreateDataSource()
DataList1.DataBind()
End If
End Sub
Function FixBoolean(ByVal inVal As Object) As String
Dim bln As Boolean
bln = CType(inVal, Boolean)
If bln Then
Return "Active"
Else
Return "Draft"
End If
End Function

Function CreateDataSource() _
As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("IsActive", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
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

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Helper function for boolean (bit)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datalist id="DataList1" runat="server">
<itemtemplate>
<asp:label id="Label1" runat="server" text='<%#
FixBoolean(eval("IsActive")) %>'></asp:label>
</itemtemplate>
</asp:datalist></div>
</form>
</body>
</html>
 
G

googlegroups

Hi Ken.

That worked a treat.
I had tried something like this but got my syntax wrong so gave up! :)

Karl
Hi,

I'd use a little helper function for that. Once you get the bit (boolean)
value, the helper function can turn it into a string that fits your need.
See the code below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
If Not IsPostBack Then
DataList1.DataSource = CreateDataSource()
DataList1.DataBind()
End If
End Sub
Function FixBoolean(ByVal inVal As Object) As String
Dim bln As Boolean
bln = CType(inVal, Boolean)
If bln Then
Return "Active"
Else
Return "Draft"
End If
End Function

Function CreateDataSource() _
As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("IsActive", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
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

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Helper function for boolean (bit)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datalist id="DataList1" runat="server">
<itemtemplate>
<asp:label id="Label1" runat="server" text='<%#
FixBoolean(eval("IsActive")) %>'></asp:label>
</itemtemplate>
</asp:datalist></div>
</form>
</body>
</html>



Hi All.

I have a datalist which pulls back a list of categories from a
database. In the returned table are the CategoryID, the CategoryName
and the IsActive Field.

CategoryID is an integer, CategoryName is a VarChar(250) and IsActive
is bit.

When the data comes back and I try to display it on the webpage,
everything works but rows with an IsActive field set to 1 (true) are
displayed as "1", and IsActive fields set to 0 are displayed as "0"
(false)

I would like to know how to change this to "Active" if a 1 is returned
or "Draft" if 0 is returned.

Thanks in advance
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top