列表视图中项目中的删除线文本 [英] strikeout text in an item in a list view

查看:81
本文介绍了列表视图中项目中的删除线文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的列表视图(在ASP.NET网站上使用C#),有两列 - 数据和警示。如果strike的值为'true',我希望'data'的内容为三角形。我的listview代码如下:

 <   asp :ListView     ID   =  ListView1    runat   =  server    DataKeyNames   =  FSID    DataSourceID   =  SqlDataSource1    GroupItemCount   =  3    OnItemDataBound   =  ListView1_ItemDataBound >  

< GroupTemplate >
< tr id = itemPlaceholderContainer runat = 服务器 >
< td id = itemPlaceholder runat = 服务器 > < / td >
< / tr >
< span class =code-keyword><
/ GroupTemplate >

< ItemTemplate >
< td runat = server style = > FSID:
< asp:标签 ID = DataLabel runat = server 文本 =' <% #Eval( 数据%> ' / >
< br / >
< asp:标签 ID = strikeLabel runat = server 文本 =' < % #Eval( 罢工%> ' / >
< br / >
< / td >
< / ItemTemplate >





代码背后我有:



  protected   void  ListView1_ItemDataBound(对象发​​件人,ListViewItemEventArgs e)
{
string strike = e.Item.FindControl( StrikeLabel)。ToString ();
if (strike == true
{
标签数据= e.Item.FindControl( DataLabel as 标签;
data.Attributes.Add( CssClass text-decoration:line-through; color:red;);
}
}





这似乎不起作用,因为我仍然在数据字段中获得正常文本< br $> b $ b

请指教。



感谢您的帮助

解决方案

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

data.Attributes["style"] = "text-decoration:line-through; color:red;";


Alternatively, you could use the Style 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))
{
    ...
}


这篇关于列表视图中项目中的删除线文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆