根据数据库值asp.net更改css值

时间:2017-02-03 19:56:44

标签: css sql asp.net

我正在使用sql server和aspx。我希望表中的值根据代码隐藏文件中的sql值更改颜色。因此,如果其中一个表中的值==“是”,则更改颜色#ff0000。如果表中的值为==“否”,则将其更改为#00ff00;调用表:submitTable,列为status。我正在使用Gridview。

<asp:GridView ID="gridlistUsers" runat="server" HorizontalAlign="left" AutoGenerateColumns="false" CssClass="table table-bordered " GridLines="None">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="TicketNumber" HeaderStyle-CssClass="Color" />
        <asp:BoundField DataField="ID" HeaderText="TicketNumber" HeaderStyle-CssClass="Color" />
        <asp:BoundField DataField="Status" HeaderText="Opened/Closed" HeaderStyle-CssClass="Color" />
    </Columns>
</asp:GridView>

4 个答案:

答案 0 :(得分:2)

你应该在asp中完成这项工作,为这个sql表创建一个REST API。 然后使用Ajax&amp; JS你可以为每个html表获取这个值并保存到变量。

然后简单地说:

"focusin"

答案 1 :(得分:1)

从你的回答中,我假设你正在使用asp:Table。

所以基本上有两种方法:

<强> 1。您的表格已经填写并呈现

如果是这种情况,请查看此表格示例:

图片:

enter image description here

.ASPX表格代码:

<asp:Table ID="submitTable" runat="server" GridLines="Both">
    <asp:TableRow runat="server" HorizontalAlign="Center" TableSection="TableHeader" VerticalAlign="Middle">
        <asp:TableCell runat="server" Width="128px">ID</asp:TableCell>
        <asp:TableCell runat="server" Width="128px">Status</asp:TableCell>
    </asp:TableRow>
</asp:Table>

这是一个填充了“是”或“否”的随机值的表。在这种情况下,您可以遍历每一行,只需应用颜色属性,如下所示:

int statusColumnIndex = 1; // Because it is the second column

int k = 0;
foreach (TableRow row in submitTable.Rows)
{
    if(k != 0) // Do this so it won't color the header row
    {
        if( row.Cells[statusColumnIndex].Text.Equals("yes") )
        {
            row.Cells[statusColumnIndex].BackColor = Color.FromArgb(255, 0, 0); // 255, 0, 0 is #ff0000 in RGB
        }

        else
        {
            row.Cells[statusColumnIndex].BackColor = Color.FromArgb(0, 255, 0); // 0, 255, 0 is ##00ff00 in RGB
        }
    }
    k++;
}

结果(不同的随机值):

enter image description here

<强> 2。如果您想在填写表格时为表格着色

在这种情况下,您可以在实例化TableCell对象以填充TableRow时为其应用颜色:

string statusValueFromSql = valueFromSqlQuery;
Color color;

if( statusValueFromSql.Equals("yes") )
{
    color = Color.FromArgb(255, 0, 0); // 255, 0, 0 is #ff0000 in RGB
}
else
{
    color = Color.FromArgb(0, 255, 0); // 0, 255, 0 is ##00ff00 in RGB
}

var cellStatus = new TableCell { BackColor = color };

最终结果与情况1完全相同。

我希望我的回答有所帮助。

答案 2 :(得分:1)

您可以使用OnRowDataBound事件。

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">

背后的代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the current row as a datarowview
        DataRowView row = e.Row.DataItem as DataRowView;

        //check the correct column value 
        if (Convert.ToBoolean(row["Status"]) == false)
        {
            //set the color of the row directly
            e.Row.BackColor = Color.Red;

            //or change it's class
            e.Row.CssClass = "Red";

            //or change a property per cell
            e.Row.Cells[0].CssClass = "Blue";
            e.Row.Cells[1].BackColor = Color.Purple;
        }
    }
}

答案 3 :(得分:1)

我已经开始工作了!这也帮助了我http://www.aspsnippets.com/Articles/Change-Background-color-of-GridView-Row-using-RowDataBound-event-in-ASPNet-using-C-and-VBNet.aspx

 <asp:GridView ID="gridlistUsers" runat="server" HorizontalAlign="left" AutoGenerateColumns="false" CssClass="table table-bordered " OnRowDataBound = "OnRowDataBound">

//代码隐藏

 protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell cell = e.Row.Cells[0];  //the 0 changes depending on the column number in sql
                string status = (cell.Text);
                string yes = "yes";
                string no = "no";

                if (status.Equals(no))
                {
                    cell.BackColor = Color.Red;
                }
                if (status.Equals(yes))
                {
                    cell.BackColor = Color.Green;
                }


            }
        }