⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynamiccontrols.aspx.cs

📁 ASP C#代码实例 适合初学人士学习使用
💻 CS
字号:
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 Example_12_14
{
	/// <summary>
	/// DynamicControls的摘要说明。
	/// </summary>
	public class DynamicControls : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button cmdAdd;
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.Label lblResult;
		protected System.Web.UI.WebControls.Label lblText;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.HtmlControls.HtmlForm Form1;
	
		private struct ControlInfo
		{
			public string ID;
			public string Type;
			public int Top;
			public int Left;
			public string EventHandler;
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!this.IsPostBack)
			{
				// Initialize controls number.
				// Used to create control id
				this.Session.Add("LastControl", 2);
			}
			else
			{
				// Recreate controls created on previous roundtrips
				RecreatePersistedControls();
			}
		}

		// Call CreateControl for each persisted control
		private void RecreatePersistedControls()
		{
			ArrayList al = (ArrayList)this.Session["DynamicControls"];
			if (al != null)
			{
				foreach (ControlInfo ci in al)
				{
					this.CreateControl(ci);
				}
			}
		}
		// Create control specified by ControlInfo structure
		private Control CreateControl(ControlInfo ci)
		{
			Control ctl = null;
			switch (ci.Type)
			{
				case "Button":
					ctl = new Button();
					((Button)ctl).Style["Position"] = "Absolute";
					((Button)ctl).Style["Top"]  = ci.Top.ToString();
					((Button)ctl).Style["Left"]  = ci.Left.ToString();
					this.AppendEvent(ctl, ci.EventHandler);
					break;
				case "TextBox":
					ctl = new TextBox();
					((TextBox)ctl).Style["Position"] = "Absolute";
					((TextBox)ctl).Style["Top"]  = ci.Top.ToString();
					((TextBox)ctl).Style["Left"]  = ci.Left.ToString();
					this.AppendEvent(ctl, ci.EventHandler);
					break;
				case "Label":
					ctl = new Label();
					((Label)ctl).Style["Position"] = "Absolute";
					((Label)ctl).Style["Top"]  = ci.Top.ToString();
					((Label)ctl).Style["Left"]  = ci.Left.ToString();
					break;
				default:
					return null;
			}
			ctl.ID = ci.ID;
			this.Form1.Controls.Add(ctl);
			return ctl;
		}
		// Set Event handler
		private void AppendEvent(Control ctl, string handler)
		{
			switch (handler)
			{
				case "Click":
					((Button)ctl).Click += new System.EventHandler(this.Button_Click);
					break;

				case "TextChanged":
					((TextBox)ctl).TextChanged += new System.EventHandler(this.TextBox_TextChanged);
					break;
			}
		}
		// Create ControlInfo structure and persist it to Session
		private ControlInfo PersistControl(string id, string type, 
			int top, int left, string eventHandler)
		{
			ControlInfo ci = new ControlInfo();
			ci.ID = id;
			ci.Type = type;
			ci.Top = top;
			ci.Left = left;
			ci.EventHandler = eventHandler;

			ArrayList al = (ArrayList)this.Session["DynamicControls"];
			if (al == null)
			{
				al = new ArrayList();
			}
			al.Add(ci);
			this.Session["DynamicControls"] = al;
			return ci;
		}
		

		#region Event Handlers for new controls
		private void Button_Click(object sender, System.EventArgs e)
		{
			this.lblResult.Text = ((Button)sender).Text + " clicked";
		}

		private void TextBox_TextChanged(object sender, System.EventArgs e)
		{
			this.lblText.Text = ((TextBox)sender).Text + " text changed";
		}

		#endregion

		private const int TOP = 96;
		private const int HEIGHT = 48;
		private const int BUTTON_LEFT = 48;
		private const int TEXTBOX_LEFT = 128;
		private void Button1_Click(object sender, System.EventArgs e)
		{
			int lastControl = (int)this.Session["LastControl"];
			int count = lastControl/2 + 1;
			int top = TOP + HEIGHT*(count - 1);
			ControlInfo ci = PersistControl("Button" + count.ToString(), 
				"Button",  
				top, BUTTON_LEFT, 
				"Click");
			Button btn = (Button)CreateControl(ci);
			btn.Text = "Button " + count.ToString();				
			ci = PersistControl("TextBox" + count.ToString(), 
				"TextBox",  
				top, TEXTBOX_LEFT, 
				"TextChanged");
			CreateControl(ci);
			this.Session["LastControl"] = count*2;
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.cmdAdd.Click += new System.EventHandler(this.Button1_Click);
			this.TextBox1.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
			this.Button1.Click += new System.EventHandler(this.Button_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -