using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;namespace 实验模板 { /// <summary> /// WebForm16 的摘要说明。 /// </summary> public class WebForm16 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Panel Panel1;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace 实验模板
{
/// <summary>
/// WebForm16 的摘要说明。
/// </summary>
public class WebForm16 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Panel Panel1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
// 在此处放置用户代码以初始化页面
TextBox a= new TextBox();
a.Text="adsdf";
Panel1.Controls.Add(a);
RadioButton b=new RadioButton();
b.Checked=true;
Panel1.Controls.Add(b);
}
} #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.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
//处理
我就是对我动态添加的单选按纽进行判断的时候,刷新页面动态生成的控件都没有拉,所以单选按钮也找不到拉,如果让动态生成的text,checkbox,一直存在啊 }
}
}
这时if(!this.IsPostBack)内的代码是不会执行的,所以你的控件会丢掉!
笨办法就是把加载控件的代码放到if(!IsPostBack)外边。
{
TextBox a= new TextBox();
Panel1.Controls.Add(a);
RadioButton b=new RadioButton();
Panel1.Controls.Add(b);
if(!this.IsPostBack)
{
a.Text="adsdf";
b.Checked=true;
}
}
这个是用户设定的,不固定啊,用户操作以后我才处理啊
if(!this.IsPostBack)
{
a.Text="adsdf";
b.Checked=true;
}
bool b = b.Checked;这种方式就得到了用户选择或输入的值。要记住,WEB FORM用 view state来保存你输入或选择的值。
bool b = b.Checked;
a,b就不存在拉,兄弟,关键是这个
在这里面添加。。动态创建的控件。。第一次进入页面时。。首先执行。。Page_Load事件。。然后是CreateChildControls()事件。。来动态添加页面控件。。当页面回发时首先执行。。CreateChildControls()创建页面控件。系统从Post回的数据中设置各个ChildControls的值了。。然后是Page_Load事件。。页面控件响应事件。。如Button_Click等。。只时当然可以。。获得回发前填入的值。。如果想重置页面只需。。调用重新创建ChildControls的方法。。因为,调用这个方法的时候,系统一定使用过EnsureChildControls()方法创建了ChildControls。(因为这个方法一般都是由页面上的某个事件触发的)这里重新创建ChildControls,系统就不会再从Post回的数据中设置各个ChildControls的值了,也就不会再恢复ChildControls的值,达到Reset的目的呵呵。。
{
/// <summary>
/// 摘要说明。
/// </summary>
public class NewPanel : System.Web.UI.WebControls.WebControl,INamingContainer
{
public NewPanel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 重载CreateChildControls()绘制页面
/// </summary>
protected override void CreateChildControls()
{
Controls.Clear();
TextBox TextBox1 = new TextBox();
TextBox1.ID = "TextBox1";
Controls.Add(TextBox1);
ClearChildViewState();
}
/// <summary>
/// </summary>
/// <param name="output"></param>
protected override void Render(HtmlTextWriter output)
{
output.Write(@"<div id=""NewPanel"">");
base.Render(output);
output.Write("</div>");
}
/// <summary>
/// 清空用户所有的输入。
/// </summary>
public void Reset()
{
Controls.Clear();
CreateChildControls();
}
}
}可以将你的程序封装成一个控件。。当然也可以将Button封装进来。并封装Button事件。。实现保存功能等。。以后直接使用这个控件模版就可以了。。增加了程序的复用性。。