加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何使用Eval更改TemplateField中的颜色

发布时间:2020-12-16 04:27:19 所属栏目:asp.Net 来源:网络整理
导读:我有一个Gridview包含许多TemplateField. 我想让每一个 td在我的html源代码中等于我的数据库中保存的颜色 我尝试代码位于下方但没有工作它给我一个 span标签内 td用我的颜色,但不要出现在浏览器上 asp:TemplateField HeaderText="BackGround Color" ItemTemp
我有一个Gridview包含许多TemplateField.
我想让每一个< td>在我的html源代码中等于我的数据库中保存的颜色
我尝试代码位于下方但没有工作它给我一个< span>标签内< td>用我的颜色,但不要出现在浏览器上
<asp:TemplateField HeaderText="BackGround Color">
     <ItemTemplate>
           <asp:Label  ID="lblBackColor" runat="server" 
                   BackColor='<%# ConvertFromHexToColor( Eval("BackColor").ToString()) %>'>
            </asp:Label>
           <itemstyle width="20%" horizontalalign="Center" />
     </ItemTemplate>    
</asp:TemplateField>

C#代码工作

public  Color ConvertFromHexToColor(string hex)
{
    string colorcode = hex;
    int argb = Int32.Parse(colorcode.Replace("#",""),NumberStyles.HexNumber);
    Color clr = Color.FromArgb(argb);
    return clr;
}

这是我的浏览器中的源html和css代码

<td>
                <span id="BodyZone__ThemesGrid_lblForeColor_0"  style="background-color: #FFFFFF;"></span>
                <itemstyle width="20%" horizontalalign="Center">
                    </itemstyle>
            </td>

CSS

table.activity_datatable td {
padding: 8px 15px;
color: #6c6c6c;
vertical-align: middle;
-webkit-transition: all 0.2s;
}

解决方法

您需要在Label内部放置文本(渲染到范围)
<asp:TemplateField HeaderText="BackGround Color">
  <ItemTemplate>
    <asp:Label  ID="lblBackColor" runat="server" BackColor='<%# ConvertFromHexToColor( Eval("BackColor").ToString()) %>'>PUT_TEXT_HERE</asp:Label>
    <itemstyle width="20%" horizontalalign="Center" />
  </ItemTemplate>
</asp:TemplateField>

您可能也更喜欢使用Panel(渲染为div)而不是Label.不要忘记将内容放入div或span中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读