110,534
社区成员
发帖
与我相关
我的任务
分享
public interface ISQL
{
EventHandler DeleteSelf
{
get;
set;
}
}
Dictionary<Guid, string> controlNameList;//存放控件名称
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["UCNameList"] != null)
{
controlNameList = (Dictionary<Guid, string>)ViewState["UCNameList"];
}
else
{
controlNameList = new Dictionary<Guid, string>();
}
SetAllControl();
}
/// <summary>
/// 设置并加载所有增加上去的用户控件
/// </summary>
private void SetAllControl()
{
if (this.controlNameList != null)
{
foreach (Guid g in this.controlNameList.Keys)
{
string controlName = this.controlNameList[g].ToString();
LoadUCControl(controlName, g);
}
}
}
/// <summary>
/// 加载单个用户控件
/// </summary>
/// <param name="controlName">控件名称</param>
/// <param name="guid">控件ID</param>
private void LoadUCControl(string controlName, Guid guid)
{
if (controlName != string.Empty)
{
UserControl uc = (UserControl)Page.LoadControl("~/UserControl/" + controlName);
if (uc != null)
{
if (uc is ISQL)
{
((ISQL)uc).DeleteSelf += RemoveControl;
uc.ID = guid.ToString();
this.PHControl.Controls.Add(uc);
}
}
}
}
/// <summary>
/// 增加一个用户控件
/// </summary>
/// <param name="controlName"></param>
private void AddControl(string controlName)
{
if (controlName != string.Empty)
{
UserControl uc = (UserControl)Page.LoadControl("~/UserControl/" + controlName);
if (uc != null)
{
if (uc is ISQL)
{
//设置ViewState
Guid guid = Guid.NewGuid();
uc.ID = guid.ToString();
controlNameList.Add(guid, controlName);
ViewState["UCNameList"] = this.controlNameList;
//加载控件
LoadUCControl(controlName, guid);
}
}
}
}
/// <summary>
/// 删除用户控件
/// </summary>
/// <param name="send"></param>
/// <param name="e"></param>
private void RemoveControl(object send, EventArgs e)
{
string deleteID = ((UserControl)send).ID;
Guid guid = new Guid(deleteID);
this.controlNameList.Remove(guid);
this.PHControl.Controls.Remove(this.PHControl.FindControl(deleteID));
}
public partial class UserControl_UCOrder : System.Web.UI.UserControl, ISQL
{
public EventHandler deleteSelf;
protected void Page_Load(object sender, EventArgs e)
{
///
}
#region ISQL Members
public EventHandler DeleteSelf
{
get
{
return this.deleteSelf;
}
set
{
this.deleteSelf = value;
}
}
#endregion
protected void BtnDelete_Click(object sender, EventArgs e)
{
if (deleteSelf != null)
{
deleteSelf(this, e);
}
}
}
public EventHandler deleteSelf;
PageLoad()
{
...
}
//页面上有个删除按钮,按钮事件
protected void BtnDelete_Click(object sender, EventArgs e)
{
if (deleteSelf != null)
{
deleteSelf(this, e);
}
}
Dictionary<Guid, string> controlNameList;//存放控件名称
private string sql;//提取语句
private string condition;//条件
private string hand;//手工数据
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["UCNameList"] != null)
{
controlNameList = (Dictionary<Guid, string>)ViewState["UCNameList"];
}
else
{
controlNameList = new Dictionary<Guid, string>();
}
SetAllControl();
}
/// <summary>
/// 设置并加载所有增加上去的用户控件
/// </summary>
private void SetAllControl()
{
if (this.controlNameList != null)
{
foreach (Guid g in this.controlNameList.Keys)
{
string controlName = this.controlNameList[g].ToString();
LoadUCControl(controlName, g);
}
}
}
/// <summary>
/// 加载单个用户控件
/// </summary>
/// <param name="controlName">控件名称</param>
/// <param name="guid">控件ID</param>
private void LoadUCControl(string controlName, Guid guid)
{
if (controlName != string.Empty)
{
UserControl uc = (UserControl)Page.LoadControl("~/UserControl/" + controlName);
if (uc != null)
{
if (uc is ISQL)
{
((ISQL)uc).DeleteSelf += RemoveControl;
uc.ID = guid.ToString();
this.PHControl.Controls.Add(uc);
}
}
}
}
private void SaveAllViewState(string controlName)
{
if (controlName != string.Empty)
{
UserControl uc = (UserControl)Page.LoadControl("~/UserControl/" + controlName);
if (uc != null)
{
if (uc is ISQL)
{
//设置ViewState
Guid guid = Guid.NewGuid();
uc.ID = guid.ToString();
controlNameList.Add(guid, controlName);
ViewState["UCNameList"] = this.controlNameList;
//加载控件
LoadUCControl(controlName, guid);
}
}
}
}
private void RemoveControl(object send, EventArgs e)
{
string deleteID = ((UserControl)send).ID;
Guid guid = new Guid(deleteID);
this.controlNameList.Remove(guid);
this.PHControl.Controls.Remove(this.PHControl.FindControl(deleteID));
}
TextBox t = new TextBox();
t.Name = "aaaaa";
this.Controls.Add(t);
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
TextBox t = (TextBox)c;
if (t.Name == "aaaaa") this.Controls.Remove(c);
}
}