日期:2014-05-20  浏览次数:20506 次

C#动态生成控件问题
举例来说:
动态生成一个TextBox,然后在页面赋值,再在button的单击事件中取到在页面输入的值,请大家给点见议!


------解决方案--------------------
TextBox textbox = new TextBox();
textbox.Text = "test ";
textbox.ID = "text ";

Button button = new Button();
button.ID = "button ";
button.OnClientClick = "alert(document.getElementById( 'text ').value);return false; ";

Control form = this.Page.FindControl( "form1 ");
form.Controls.Add(textbox);
form.Controls.Add(button);
------解决方案--------------------
System.Web.UI.WebControls.TextBox tb = new System.Web.UI.WebControls.TextBox();
tb.Text = "g a m e ";
Page.Form.FindControl( "form1 ").Controls.Add(tb);
------解决方案--------------------
TextBox textbox = new TextBox();
textbox.Text = "test ";
textbox.ID = "text ";

Button button = new Button();
button.ID = "button ";
button.Click += new EventHandler(button_Click);

Control form = this.Page.FindControl( "form1 ");
form.Controls.Add(textbox);
form.Controls.Add(button);


button_Click:

TextBox textbox = (TextBox)this.FindControl( "text ");
Response.Write(textbox.Text);