简体   繁体   English

ASP.NET如何在单击按钮+行条目时更改表行的背景颜色?

[英]ASP.NET how to change background color of table row on button click + row entry?

I have a table with one row which is filled in with values after the user clicks a button. 我有一张表格,其中有一行,在用户单击按钮后,该表格就会填充值。 I want to change the background color of the row to different colors based on the values that are filled in to the row. 我想根据填充到行中的值将行的背景色更改为不同的颜色。

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" />
<tr>
    <td runat="server" id ="td1" class="auto-style1"></td>
    <td runat="server" id ="td2" class="auto-style1"></td>
    <td runat="server" id ="td3" class="auto-style1"></td>
    <td runat="server" id ="td4" class="auto-style1"></td>
</tr>

and my Button1_Click function looks like 我的Button1_Click函数看起来像

protected void Button1_Click(object sender, EventArgs e)
{
    string[]toDisp = someFunction();
    td1.InnerText = toDisp[0];
    td2.InnerText = toDisp[1];
    td3.InnerText = toDisp[2];
    td4.InnerText = toDisp[3];
}

Basically, I want to set the background color of the table row based on the value of toDisp[1]. 基本上,我想基于toDisp [1]的值设置表行的背景色。 How should I go about doing this? 我应该怎么做呢? Thanks. 谢谢。

If its going to be just 1 row 如果只是一排

just set a ID to it with runat attribute 只需使用runat属性为其设置ID

<tr id="test" runat="server">
    <td runat="server" id ="td1" class="auto-style1"></td>
    <td runat="server" id ="td2" class="auto-style1"></td>
    <td runat="server" id ="td3" class="auto-style1"></td>
    <td runat="server" id ="td4" class="auto-style1"></td>
</tr>

then based on the condition of toDisp[1] 然后根据toDisp [1]的条件

you can write a switch statement or Random (based on color requirement) to just set 您可以编写一个switch语句或Random(根据颜色要求)进行设置

test.BgColor = "SomeColor";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM