Solved

Datalist Conditional Values

Posted on 2004-10-29
318 Views
Last Modified: 2012-05-05
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
Question by:g9support
    13 Comments
     
    LVL 17

    Accepted Solution

    by:
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    assuming dg is your datagrid and its name column ordinal is 1, adjust for your column ordinal

    0
     

    Author Comment

    by:g9support
    I'm using a datalist though. Does tthe following still apply?
    0
     
    LVL 15

    Expert Comment

    by:Thogek
    Or, something funny-looking like:

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

    (assuming C# syntax).
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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
     

    Author Comment

    by:g9support
    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
     
    LVL 18

    Assisted Solution

    by:tusharashah
    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
     
    LVL 15

    Expert Comment

    by:Thogek
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    ok your gonna have to use findcontrol, sorry bout that:
     Dim PriceLabel As Label = _
                    CType(e.Item.FindControl("PriceLabel"), Label)
    0
     

    Author Comment

    by:g9support
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now