给你一段
HtmlTableRow row3 = new HtmlTableRow();
row3.Align = "center";
Button Btn = new Button();
Btn.ID = "Btn_0";
Btn.Width = 60;
Btn.Height = 20;
Btn.Text = "确 定";
HtmlTableCell cell20 = new HtmlTableCell();
cell20.ColSpan = 2;
cell20.Controls.Add(Btn);
row3.Cells.Add(cell20);
ta.Rows.Add(row3);
Btn.Click +=new EventHandler(NewButton_Click);

解决方案 »

  1.   

    用response.write不知道是否可行,这里有个替代的方法:
    //====================
    //WebForm2.aspx:
    //====================<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm2" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="MyForm" method="post" runat="server">
    &nbsp;
    </form>
    </body>
    </HTML>//===========================
    //WebForm2.aspx.cs:
    //===========================
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace WebApplication2
    {
    /// <summary>
    /// WebForm2 的摘要说明。
    /// </summary>
    public class WebForm2 : System.Web.UI.Page
    {
    protected HtmlForm MyForm;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!IsPostBack)
    {
    Button btn=new Button();
    btn.Text="动态添加的按钮";
    MyForm.Controls.Add(btn); }
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.ID = "MyForm1";
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }
      

  2.   

    IsPostBack表示页面是否是回发,其实在示例代码应该去掉这个条件判断,否则,一按按钮,页面重新加载后,动态加载的按钮就没有了,你可以分别加上这个条件判断和去掉它试试,就知道它是什么意思了。
      

  3.   

    用Response.Write()方法只能生成普通的html文本,不能创建控件,要动态创建控件,必须用sjc0(流浪者) 那样的方法。
      

  4.   

    agree with  webdiyer(陕北吴旗娃) &jjcccc() ( ) 
      

  5.   

    Just do it as sjc0(流浪者) told u