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?
g9supportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.