c# - 如何为 HTMLTable 中的某些特定单元格着色?

标签 c# asp.net

我为我的公司开发了一个培训矩阵,向所有员工展示了公司提供的所有培训类(class)。现在,我必须以易于管理员编辑和更新的方式开发它。我做的一切都是正确的,除了一件事是为每组类(class)赋予特定的颜色(因为我有 3 种类型的类(class)),从第四个单元格到最后一个单元格。仅供引用,我有两个 SqlDataSources 用于开发此矩阵:

SqlDataSource1 用于检索 GroupID:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                SelectCommand="SELECT [ID] FROM [groups]"></asp:SqlDataSource>

SqlDataSource2 将从 SqlDataSource1 获取 GroupID 并使用它来生成矩阵:

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
                                            ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                                            SelectCommandType="StoredProcedure" SelectCommand="kbiReport" FilterExpression="[Division] like '{0}%'">

                            <SelectParameters>
                                <asp:Parameter  Name="GroupID"/>
                            </SelectParameters>

                            <FilterParameters>
                                <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName"
                                                         PropertyName="SelectedValue" Type="String" />
                            </FilterParameters>

            </asp:SqlDataSource>

现在,由于我使用的是 HTMLTable,因此我需要访问 SqlDataSource1 来执行一些逻辑,例如: 如果 GroupID = 1,则为该组指定蓝色,依此类推。我可以通过执行以下操作来做到这一点:

我的 C# 隐藏代码:

protected void Page_Load(object sender, EventArgs e)
    {

        DataView dv2 = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        foreach (DataRowView group in dv2)
        {
            SqlDataSource2.SelectParameters[0].DefaultValue = group[0].ToString();
            //create a new HtmlTable object
            HtmlTable table = new HtmlTable();

            DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
            int columns = dv.Table.Columns.Count;
            int rows = dv.Count;

            //table's formating-related properties
            table.Border = 2;
            table.CellPadding = 3;
            table.CellSpacing = 3;
            table.Width = "900px";

            //to get the css style
            table.Attributes["class"] = "uGrid";

            //create a new HtmlTableRow and HtmlTableCell objects
            HtmlTableRow row;
            HtmlTableRow header = new HtmlTableRow();
            HtmlTableCell cell;


            //for adding the headers to the table
            foreach (DataColumn column in dv.Table.Columns)
            {
                HtmlTableCell headerCell = new HtmlTableCell("th");
                headerCell.InnerText = column.Caption;

                //The following if-else statements are for checking the GroupID and give each group
                //a specific color
                if (group[0].ToString().Equals("1"))
                    headerCell.BgColor = "lightBlue";
                else if (group[0].ToString().Equals("2"))
                    headerCell.BgColor = "lightYellow";
                else if (group[0].ToString().Equals("3"))
                    headerCell.BgColor = "Orange";

                //the header cells to the header
                header.Cells.Add(headerCell);
            }
            table.Rows.Add(header);

            //loop for adding rows to the table
            foreach (DataRowView datarow in dv)
            {
                row = new HtmlTableRow();
                //row.BgColor = "yellow";


                //loop for adding cells
                for (int j = 0; j < columns; j++)
                {
                    cell = new HtmlTableCell();
                    if (j < 4)
                    {
                        cell.InnerText = datarow[j].ToString();
                    }
                    else
                    {

                        CheckBox checkbox = new CheckBox();

                        int checkBoxColumns = dv.Table.Columns.Count - 5;
                        string fieldvalue = datarow[j].ToString();
                        string yes = fieldvalue.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[1];
                        string courseid = fieldvalue.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[0];
                        checkbox.ID = row.Cells[3].InnerText + "," + courseid.Trim();
                        checkbox.Checked = yes.Equals("Yes");
                        cell.Controls.Add(checkbox);

                    }

                    //add the cell to the current row
                    row.Cells.Add(cell);
                }

                //add the row to the table
                table.Rows.Add(row);
            }

            //add the table to the page
            PlaceHolder1.Controls.Add(table);

        }
    }

每组有不同数量的类(class),但着色应从第四个单元格开始到最后一个单元格。我在上面代码的以下部分做了很多尝试,但失败了,我不知道为什么。 那么如何做到这一点?

//for adding the headers to the table
            foreach (DataColumn column in dv.Table.Columns)
            {
                HtmlTableCell headerCell = new HtmlTableCell("th");
                headerCell.InnerText = column.Caption;

                //The following if-else statements are for checking the GroupID and give each group
                //a specific color
                if (group[0].ToString().Equals("1"))
                    headerCell.BgColor = "lightBlue";
                else if (group[0].ToString().Equals("2"))
                    headerCell.BgColor = "lightYellow";
                else if (group[0].ToString().Equals("3"))
                    headerCell.BgColor = "Orange";

                //the header cells to the header
                header.Cells.Add(headerCell);
            }
            table.Rows.Add(header);

最佳答案

我会使用 ListView 控件构建 html 表来解决这个问题。使用 ListView 控件,您可以访问 OnItemDataBound 属性,您可以使用该属性编写代码来为您需要的特定行着色。

关于c# - 如何为 HTMLTable 中的某些特定单元格着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9292611/

相关文章:

C#/ASP.NET 遍历表

ASP.net ViewState - 即使禁用,也存在一些 View 状态。为什么?

asp.net - 自动辅助功能测试/屏幕阅读器模拟器

c# - Html.DisplayFor 编译错误 - MVC 2

c# - system.Decimal 是否使用比 'decimal' 更多的内存?

c# - 当 ListView 被禁用时,在 ListView 中启用分页。 ASP.NET C#

javascript - ASP.NET 中的 x-frame-option SAMEORIGIN 和点击劫持

c# - System.Net.HttpWebRequest 无限工作

c# - 获取可用的可用 RAM 内存 C#

c# - 从事件触发更改时,MVVM 绑定(bind)不起作用