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