更新时间:2023-02-02 22:28:38
HTML不包含名为CssClass
的属性。
ASP.NET控件包含 property 名为CssClass
,它映射到名为class
的HTML属性。此属性包含CSS文件中定义的类名列表。这些CSS类的样式将应用于元素。
您提供的值不是CSS类名;它是内联样式定义。因此,您需要使用样式
属性:data.Attributes [ style] = text-decoration:line-through; color:red;;
或者,您可以使用Style
property:data.Style [ text-decoration] = line-through;
data.Style [ color] = red;
你也会需要检索StrikeLabel控件的文本;在它上面调用.ToString()
只会返回类型名称。
文本很可能是True
,这与true
的值不同,因此您需要修改测试。标签strikeLabel =(标签)e.Item.FindControl( StrikeLabel跨度>);
string strike =(strikeLabel!= null )? strikeLabel.Text: string .Empty;
if ( string .Equals(strike, True,StringComparison.OrdinalIgnoreCase))
{
...
}
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:
<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:
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
HTML does not contain an attribute calledCssClass
.
ASP.NET controls contain a property calledCssClass
, which maps to the HTML attribute calledclass
. 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 thestyle
attribute:data.Attributes["style"] = "text-decoration:line-through; color:red;";
Alternatively, you could use theStyle
property: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.Label strikeLabel = (Label)e.Item.FindControl("StrikeLabel"); string strike = (strikeLabel != null) ? strikeLabel.Text : string.Empty; if (string.Equals(strike, "True", StringComparison.OrdinalIgnoreCase)) { ... }