|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Conditional True/False output for datalist itemHi 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 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> <googlegro***@tlbsolutions.com> wrote in message Show quoteHide quote news:1156177273.386331.21290@h48g2000cwc.googlegroups.com... > 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 > Hi Ken.
That worked a treat. I had tried something like this but got my syntax wrong so gave up! :) Karl Ken Cox [Microsoft MVP] wrote: Show quoteHide quote > 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> > > > > <googlegro***@tlbsolutions.com> wrote in message > news:1156177273.386331.21290@h48g2000cwc.googlegroups.com... > > 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 > >
error: can't find control 'mylabel'
which expert can finally solve this? data inserted again after refresh Center Login Control and Firefox Admin front end generator How to access a server control within a repeater control Using Control.Render() from a page with a MasterPage tabbed pane mailing labels with asp.net report tool Can't use standard controls - drag and drop fails |
|||||||||||||||||||||||