CssClass不起作用

时间:2014-11-02 18:04:22

标签: 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;
}

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

1 个答案:

答案 0 :(得分:0)

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