Tar*_*ied 8 html css asp.net gridview
我有一个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>
Run Code Online (Sandbox Code Playgroud)
C#代码工作
public Color ConvertFromHexToColor(string hex)
{
string colorcode = hex;
int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
Color clr = Color.FromArgb(argb);
return clr;
}
Run Code Online (Sandbox Code Playgroud)
这是我的浏览器中的源html和css代码
<td>
<span id="BodyZone__ThemesGrid_lblForeColor_0" style="background-color: #FFFFFF;"></span>
<itemstyle width="20%" horizontalalign="Center">
</itemstyle>
</td>
Run Code Online (Sandbox Code Playgroud)
CSS
table.activity_datatable td {
padding: 8px 15px;
color: #6c6c6c;
vertical-align: middle;
-webkit-transition: all 0.2s;
}
Run Code Online (Sandbox Code Playgroud)
如果要检查布尔值是否为true,则将显示绿色,否则将显示红色。然后根据评估功能显示具有相应颜色的文本。在这里,GetStatus是一种方法,您需要在后面的代码中创建它,并且将文本绑定到UI,否则,您可以照常使用Eval或Bind函数进行绑定。
ForeColor='<%# (bool)Eval("UserType")==true?System.Drawing.Color.Green:System.Drawing.Color.Red %>'
Text='<%# GetStatus((bool)Eval("UserType")) %>'>
Run Code Online (Sandbox Code Playgroud)
您需要将文本放置在标签内(呈现为跨度)
<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>
Run Code Online (Sandbox Code Playgroud)
您可能还更喜欢使用面板(呈现为 div)而不是标签。不要忘记将内容放入 div 或 span 内。