CssClass赢得';不起作用

本文关键字:不起作用 赢得 CssClass | 更新日期: 2023-09-27 18:28:58

我正在构建一个表并动态放置乘法按钮。问题是,我也给了他们一个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;
}

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

CssClass赢得';不起作用

我试过你的代码,在输出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++;
        }
    }
}