【问题标题】:Creating a dynamic table创建动态表
【发布时间】:2012-05-03 12:16:20
【问题描述】:

我正在使用 C# 来发展我的技能。我正在尝试我在网上找到的教程:http://geekswithblogs.net/dotNETvinz/archive/2009/03/17/dynamically-adding-textbox-control-to-aspnet-table.aspx 但是当我尝试代码并添加 Generate table 方法时,它告诉我找不到Table table = new Table(); 的类型或命名空间名称.. 有谁知道我应该为此使用的命名空间。这是剩下的代码:

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

任何帮助将不胜感激,谢谢

【问题讨论】:

    标签: c# .net visual-studio-2010 dynamic


    【解决方案1】:

    确保您正在导入表 MSDN Ref 的命名空间

    有没有

    using System.Web.UI.WebControls;
    

    在文件的顶部?

    【讨论】:

      【解决方案2】:

      不幸的是,教程通常会忽略他们正在使用的库,这可能会令人不安。但是有一个简单的解决方法:谷歌你得到一个错误,在这种情况下C# Table,第三个结果说:

      表格类 (System.Web.UI.WebControls) - MSDN - Microsoft

      括号之间是你需要包含的库:

      @using System.Web.UI.WebControls; // for .cshtml pages
      using System.Web.UI.WebControls; // for .cs pages
      

      始终在搜索中包含 C#,并查找“MSDN”搜索结果。

      【讨论】:

        猜你喜欢
        • 2018-08-12
        • 1970-01-01
        • 2017-02-25
        • 2016-08-20
        • 2016-10-14
        • 2017-01-31
        • 2011-12-03
        • 1970-01-01
        相关资源
        最近更新 更多