c# - CssClass 不起作用

标签 c# css class button dynamic

我正在构建一个表格并动态放置乘法按钮。问题是我也给了他们一个 CSS 类来使用,但它不起作用,我不知道为什么?!

这是代码

protected void Page_Load(object sender, EventArgs e)
{
    int rowNum = 4;
    int cellNum = 10;
    int idcell = 1;
    int idrow = 1;

    for (int i = 0; i < rowNum; i++)
    {
        TableRow aNewRow = new TableRow();
        for (int j = 0; j < cellNum; j++)
        {
            TableCell aNewCell = new TableCell();
            Button aNewButton = new Button();

            aNewButton.Click += new System.EventHandler(this.Button_Click_First_Row);
            aNewButton.ID = "R" + idrow + "B" + idcell;
            aNewButton.Text = "Boka";
            aNewButton.CssClass = "dynamicbuttons";

            aNewCell.Controls.Add(aNewButton);
            aNewRow.Cells.Add(aNewCell);
            idcell++;
        }
        Table1.Rows.Add(aNewRow);
        idrow++;
    }
}

和我的 CSS 类

.dynamicbuttons {
    background-color:green;
    width:105px;
}

我不知道为什么它不起作用。

最佳答案

我试过你的代码,我在输出的 html 中得到了 css 类,所以可能是你环境中的其他东西导致了问题。

我的测试标记:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        .dynamicbuttons {
            background-color:green;
            width:105px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="Table1" runat="server"></asp:Table>
    </div>
    </form>
</body>
</html>

和代码背后:

public partial class TableForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int rowNum = 4;
        int cellNum = 10;
        int idcell = 1;
        int idrow = 1;

        for (int i = 0; i < rowNum; i++)
        {
            TableRow aNewRow = new TableRow();
            for (int j = 0; j < cellNum; j++)
            {
                TableCell aNewCell = new TableCell();
                Button aNewButton = new Button();

                //aNewButton.Click += new System.EventHandler(this.Button_Click_First_Row);
                aNewButton.ID = "R" + idrow + "B" + idcell;
                aNewButton.Text = "Boka";
                aNewButton.CssClass = "dynamicbuttons";

                aNewCell.Controls.Add(aNewButton);
                aNewRow.Cells.Add(aNewCell);
                idcell++;
            }
            Table1.Rows.Add(aNewRow);
            idrow++;
        }
    }
}

关于c# - CssClass 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26702567/

相关文章:

c# - 使用 Entity Framework Code First 4.3 创建存储库

html - iOS 9 电子邮件 - 文本重叠图像横幅

java - 在哪里添加java类的成员

python - 为什么在 Python 中从不同路径调用类时 __class__ 不同?

c++ - 关键字 'class' 和 c++ 中的类名之间可以有什么吗?

c# - c#中的静态变量是否有等价于 "this"

c# - 如何在 linq 连接 (lambda) 上添加 where 子句?

javascript - 在 Bootstrap 中水平制作三个 div 的全宽和高度

css - 无法在 CSS 中生成此字体

javascript - 如何获取 DropDownList 选定值并将其发送到 ASP.NET MVC 4 中的 Controller 并使用 JavaScript?