• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

Datalist Conditional Values

Referencing this topic: http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21188066.html

I have a column that has either a 1  or a 0.

If the value is 1 i want to display an asterik "*" if not, then display nothing.

But i'm using  <%# DataBinder.Eval(Container.DataItem, "Name") %>

How would I do this?
0
g9support
Asked:
g9support
  • 6
  • 3
  • 2
  • +1
2 Solutions
 
AerosSagaCommented:
Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemDataBound
        Dim decTempSum, decFinalTotal As Decimal
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim Name As String = (DataBinder.Eval(e.Item.DataItem, "Name"))
            If Name = "1" Then
            e.Item.Cells(1).Text = "*"
            Else
            e.Item.Cells(1).Text = " "
         End If
    End Sub

Aeros
0
 
AerosSagaCommented:
assuming dg is your datagrid and its name column ordinal is 1, adjust for your column ordinal

0
 
g9supportAuthor Commented:
I'm using a datalist though. Does tthe following still apply?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ThogekCommented:
Or, something funny-looking like:

    <%# ( DataBinder.Eval(Container.DataItem, "Name") == 1 ? "*" : "" ) %>

(assuming C# syntax).
0
 
tusharashahCommented:
I would do it in ItmDataBound of DataList event like following:
You can do similar thing in datalist also:

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
      switch( e.Item.ItemType )
      {
      case ListItemType.Item:
      case ListItemType.AlternatingItem:
            Label lbl = ((Label) e.Item.FindControl( "Label1" ));  // Replace with your TextBox/Label or whatever you have
                       if( lbl.Text == 1 )
                        lbl.Text = "*";
      break;
      }
}

-tushar
0
 
AerosSagaCommented:
oops typo:

Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim Name As String = (DataBinder.Eval(e.Item.DataItem, "Name"))
            If Name = "1" Then
            e.Item.Cells(1).Text = "*"
            Else
            e.Item.Cells(1).Text = " "
         End If
    End Sub
0
 
g9supportAuthor Commented:
Here's what I'm doing:

   Private Sub dlEmpHistory_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlEmpHistory.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim Name As String = (DataBinder.Eval(e.Item.DataItem, "Name"))
            If Name = "1" Then
                e.Item.Cells(1).Text = "*"
            Else
                e.Item.Cells(1).Text = " "
            End If
        End If
    End Sub

But the IDE enviroment states I "Cells" nor "Cell" is a member of 'System.Web.UI.WebControls.DataListItem

How can do this with a DL?

Thanks, I'm almost there! Thank you all for your help.
0
 
tusharashahCommented:
What is that you're using to Display Name inside your DataList?

You'll have to retrieve that value like following:
    Dim lbl As Label = CType(e.Item.FindControl("Label1"), Label)
    Dim tb As TextBox = CType(e.Item.FindControl("TextBox1"), TextBox)

-tushar
0
 
ThogekCommented:
That still seems like a lot of work for a simple formatting that could be done inside of the <%#...%> block, but... it should certainly work, so...
0
 
AerosSagaCommented:
ok your gonna have to use findcontrol, sorry bout that:
 Dim PriceLabel As Label = _
                CType(e.Item.FindControl("PriceLabel"), Label)
0
 
g9supportAuthor Commented:
I'm just putting it directly on the page. i'm not putting it inside of another control.

After reading your post it seems as if I need to put it in another control then?
0
 
AerosSagaCommented:
just make sure your using template columns like so:
<asp:DataList id="ItemsList"
           BorderColor="black"
           CellPadding="5"
           CellSpacing="5"
           RepeatDirection="Vertical"
           RepeatLayout="Table"
           RepeatColumns="3"
           ShowBorder="True"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <HeaderTemplate>

            List of items

         </HeaderTemplate>
               
         <ItemTemplate>

            Description: <br>
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>

            <br>

            Price:
            <asp:Label id="PriceLabel"
                 runat="server"/>

         </ItemTemplate>
 
      </asp:DataList>
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now