我的程序用System.Web.UI.WebControls.Table Table1动态生成一个表格,
                           TableRow r = new TableRow();
TableCell c = new TableCell();
c.Controls.Add(new LiteralControl("选择题ID"));
r.Cells.Add(c);
c = new TableCell();
c.Controls.Add(new LiteralControl("题干"));
r.Cells.Add(c);
c = new TableCell();
c.Controls.Add(new LiteralControl("难度系数"));
r.Cells.Add(c);
c = new TableCell();
c.Controls.Add(new LiteralControl("知识点"));
r.Cells.Add(c);
Table1.Rows.Add(r);
现在的问题是想把第一个控件改成添加一个LinkButton,LinkButton需要runat=server这个怎么在程序动态解决,
这是错误提示:
System.Web.HttpException: 类型“LinkButton”的控件“_ctl0”必须放在具有 runat=server 的窗体标记内。

解决方案 »

  1.   

    给你一个参考:(开发一个TextBox控件)using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace CompositionSampleControls {    public class Composition1 : Control, INamingContainer {        public int Value {
               get {
                   this.EnsureChildControls();
                   return Int32.Parse(((TextBox)Controls[1]).Text);
               }
               set {
                   this.EnsureChildControls();
                   ((TextBox)Controls[1]).Text = value.ToString();
               }
            }        protected override void CreateChildControls() {            this.Controls.Add(new LiteralControl("<h3>" + "值:"));            TextBox box = new TextBox();
                box.Text = "0";
                this.Controls.Add(box);            this.Controls.Add(new LiteralControl("</h3>"));
            }
        }
    }
      

  2.   

    楼上这个没用,我需要的是在页面动态生成按钮,asp.net要求控件有runat=server才能在页面运行,而控件在cs文件中只能设置一些属性,我不知道怎么在cs文件中实现这个runat=server??