2014-11-02 54 views
0

我正在建立一个表和动态放置乘法按钮。问题是,我也给他们一个CSS类来使用,但它不会工作,我不知道为什么?!CssClass将无法工作

下面是代码

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; 
} 

我不知道为什么它不会工作。

+0

是输出HTML的CSS类?在浏览器中右键单击显示超文本 – CodeFanatic 2014-11-02 18:07:52

+0

不,它不是。根本不显示。 – Bojje 2014-11-02 18:09:50

回答

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++; 
     } 
    } 
} 
+0

是的,我不知道为什么它不起作用。我创建了一个新的CSS工作表,并将其链接到我的CssClass中,它的工作方式就像一个魅力一样...... – Bojje 2014-11-02 21:24:41