Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple listview (in ASP.NET website using C#) with two columns - data and strike. If the value of strike is 'true', i want the contents of 'data' to strikeout. My listview code is as follows:
XML
<asp:ListView ID="ListView1" runat="server" DataKeyNames="FSID" DataSourceID="SqlDataSource1" GroupItemCount="3" OnItemDataBound="ListView1_ItemDataBound">

            <GroupTemplate>
                <tr id="itemPlaceholderContainer" runat="server">
                    <td id="itemPlaceholder" runat="server"></td>
                </tr>
            </GroupTemplate>

            <ItemTemplate>
                <td runat="server" style="">FSID:
                   <asp:Label ID="DataLabel" runat="server" Text='<%# Eval("Data") %>' />
                    <br />
                    <asp:Label ID="strikeLabel" runat="server" Text='<%# Eval("strike") %>' />
                    <br />
               </td>
            </ItemTemplate>


In code behind i have:

C#
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        string strike = e.Item.FindControl("StrikeLabel").ToString();
        if(strike == "true")
        {
            Label data = e.Item.FindControl("DataLabel") as Label;
            data.Attributes.Add("CssClass", "text-decoration:line-through; color:red;");
        }
    }


This does not seem to work, as i still get normal text in data filed

Kindly advise.

Thanks for your help
Posted

1 solution

HTML does not contain an attribute called CssClass.

ASP.NET controls contain a property called CssClass, which maps to the HTML attribute called class. This attribute contains a list of class names defined in your CSS file(s). The styles for those CSS classes will be applied to the element.

The value you're supplying is not a CSS class name; it is an inline style definition. Therefore, you need to use the style attribute:
C#
data.Attributes["style"] = "text-decoration:line-through; color:red;";

Alternatively, you could use the Style property:
C#
data.Style["text-decoration"] = "line-through";
data.Style["color"] = "red";


You'll also need to retrieve the text of the "StrikeLabel" control; calling .ToString() on it will only return the type name.

The text will most likely be "True", which is not the same value as "true", so you'll need to modify your test.
C#
Label strikeLabel = (Label)e.Item.FindControl("StrikeLabel");
string strike = (strikeLabel != null) ? strikeLabel.Text : string.Empty;
if (string.Equals(strike, "True", StringComparison.OrdinalIgnoreCase))
{
    ...
}
 
Share this answer
 
v2
Comments
Member 10235977 24-Sep-15 14:22pm    
thanks but it has not worked. I tried both the alternatives.
Richard Deeming 24-Sep-15 14:32pm    
Debug the code and check what the text of the "StrikeLabel" is. It's probably "True", which is not the same as "true".

if (strike == "True") ...
Richard Deeming 24-Sep-15 14:32pm    
Also, you need to retrieve the text of the "StrikeLabel" control. Calling .ToString() on it will simply return its type name.
Member 10235977 24-Sep-15 14:53pm    
thanks. it has worked

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