Click here to Skip to main content
15,908,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to change color of a datalist cell based on lablels text inside in it
Posted

1 solution

Try this sample:

C#
public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Rows.Add("aaa");
            dt.Rows.Add("bbbb");
            dt.Rows.Add("aaa");
            DataList1.DataSource = dt;
            DataList1.DataBind();
        }
        

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item )
            {
                Label label = e.Item.FindControl("NameLabel") as Label;
                if (label.Text == "aaa")
                    e.Item.BackColor = System.Drawing.Color.Red; 
             
            }
        }
    }



ASPX:

XML
<asp:DataList ID="DataList1" runat="server"
        onitemdatabound="DataList1_ItemDataBound">
        <HeaderTemplate>
            Student Information
        </HeaderTemplate>
        <ItemTemplate>
            Name:
            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' ForeColor="#000099" />
            <br />
        </ItemTemplate>

    </asp:DataList>
 
Share this answer
 
Comments
Member 10517120 31-Jan-14 1:11am    
tnx can we check the txt in .aspx so that no need to write item databound
Karthik_Mahalingam 31-Jan-14 1:17am    
then you should go for JQuery or javascript , its bit complicated..
Member 10517120 31-Jan-14 1:28am    
ok

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900