当前位置: 代码迷 >> ASP.NET >> C#动态生成控件有关问题
  详细解决方案

C#动态生成控件有关问题

热度:3985   发布时间:2013-02-26 00:00:00.0
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);
  相关解决方案