问题
I'm working with C# to develop my skills. I am trying a tutorial that I have found online: http://geekswithblogs.net/dotNETvinz/archive/2009/03/17/dynamically-adding-textbox-control-to-aspnet-table.aspx but when I try the code and add the Generate table method it tells me that the type or the namespace name for Table table = new Table(); could not be found.. does anyone know what namespace I should use for this. This is the rest of the code:
private void GenerateTable(int colsCount, int rowsCount)
{
//Creat the Table and Add it to the Page
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
// Now iterate through the table and add your controls
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
// Set a unique ID for each TextBox added
tb.ID = "TextBoxRow_" + i + "Col_" + j;
// Add the control to the TableCell
cell.Controls.Add(tb);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
// Add the TableRow to the Table
table.Rows.Add(row);
}
}
any help would be much appreciated thanks
回答1:
Make sure that you are importing the namespace for table MSDN Ref
is there a
using System.Web.UI.WebControls;
at the top of your file?
回答2:
Unfortunately it's common for tutorials to leave out the libraries they're using, and it can be unnerving. But there's an easy fix: Google what you're getting an error on, in this case C# Table, and the third result down says:
Table Class (System.Web.UI.WebControls) - MSDN - Microsoft
And in between the parenthesis is the library you need to include:
@using System.Web.UI.WebControls; // for .cshtml pages
using System.Web.UI.WebControls; // for .cs pages
Just always include C# in your search, and look for an 'MSDN' Search Result.
来源:https://stackoverflow.com/questions/10271171/creating-a-dynamic-table