后台代码里写:string sButtons = "<asp:Button ID=\"btnDelete\" runat=\"server\" Text=\"删除\" Visible=\"true\" OnClick=\"BtnDelete_Click\"/>
sTable ="<table><tr><td>"+sButtons+ "</td></tr></table>"; 
和   protected void DeleteButton_Click(object sender, EventArgs e)
    {
        Response.Write("看看我行吧!!!!!");
    }
可以实现可以吗?我想在点了click后触发事件.
结果呢页面根本不能显示出来控件.为什么呢?
总之,我就是想动态生成一服务器button控件,并给他添加attribuite的click事件.期待各位高手指点了......

解决方案 »

  1.   

    那你不能这样添加了吧<td id="td1" runat="server"></td>Button  bt1 = new Button();
    bt1.ID =  "";
    .....
    bt1.Onclick =  ....
    td1.Controls.Add(bt1);
      

  2.   

     protected void Button1_Click(object sender, EventArgs e)
            {            if (ViewState["controls"] == null) { ViewState["controls"] = new System.Collections.Generic.List<string>(); }            System.Collections.Generic.List<string> list = (System.Collections.Generic.List<string>)ViewState["controls"];
                list.Add(System.IO.Path.GetRandomFileName());
                ViewState["controls"] = list;            foreach (string item in list)
                {
                    HtmlButton btn = new HtmlButton();
                    btn.Attributes.Add("onclick", string.Format("window.alert('{0}')",item));
                    btn.InnerText = item;
                    PlaceHolder1.Controls.Add(btn);
                }
                
            }