当前位置:编程学习 > C#/ASP.NET >>

如何在Table中动态添加用户自定义控件

Protected void Button1_Click (object sender, System.EventArgs e)
{
   // Total number of rows.
   int rowCnt;
   // Current row count.
   int rowCtr;
   // Total number of cells per row (columns).
   int cellCtr;
   // Current cell counter.
   int cellCnt;


   rowCnt = int.Parse(TextBox1.Text);
   cellCnt = int.Parse(TextBox2.Text);

   for(rowCtr=1; rowCtr <= rowCnt; rowCtr++) {
      // Create a new row and add it to the table.
      TableRow tRow = new TableRow();
      Table1.Rows.Add(tRow);
      for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++) {
         // Create a new cell and add it to the row.
         TableCell tCell = new TableCell();
         tRow.Cells.Add(tCell);               
         // Mock up a product ID.
         string prodID = rowCtr + "_" + cellCtr;

         // Add a literal text as control.
         tCell.Controls.Add(new LiteralControl("Buy: "));
         // Create a Hyperlink Web server control and add it to the cell.
         System.Web.UI.WebControls.HyperLink h = new HyperLink();
         h.Text = rowCtr + ":" + cellCtr;
         h.NavigateUrl = "http://www.microsoft.com/net";
         tCell.Controls.Add(h);
      }
   }
}

    上面是MSDN上的关于Table类的实例,现在我想在动态生成的表的单元格中添加一个用户自定义控件而不是上面例子中服务器控件,应该怎么办啊????哪位高手帮帮忙! --------------------编程问答-------------------- 用户自定义控件? 只要是服务器端控件都可以添加啊 --------------------编程问答-------------------- HtmlGenericControl hgc = new HtmlGenericControl("div");
                    hgc.Attributes.Add("Id", "div" + i);
 tCell.Controls.Add(hgc); 这个行不? --------------------编程问答-------------------- TemplateControl.LoadControl("~/control/JobReportPage.ascx");  
Control myControl = Page.LoadControl("~/control/JobReportPage.ascx"); 
tCell.Controls.Add(myControl);

用上面的代码将用户自定义控件添加好了,但是无法调用用户自定义控件的属性和方法,怎么办啊?? --------------------编程问答-------------------- 终于解决了,代码贴一下

for (int i = 0; i < count; i++)
            {
                TableRow tRow = new TableRow();
                Table1.Rows.Add(tRow);
                for (int j = 0; j < 2; j++)
                {
                    TableCell tCell = new TableCell();
                    tRow.Cells.Add(tCell);
                    //string prodID = i + "_" + j;
                    tCell.Controls.Add(new LiteralControl(""));
                    ASP.control_jobreportpage_ascx myControl = (ASP.control_jobreportpage_ascx)Page.LoadControl("~/control/JobReportPage.ascx");
                    myControl.Name = this.name;
                    if(k<rowCount)
                    {
                        myControl.Date = dt[k++];
                        myControl.GetDataTable();
                        myControl.getJobPlan();
                    }
                   
                    tCell.Controls.Add(myControl);
                }

            }
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,