62,046
社区成员
发帖
与我相关
我的任务
分享
protected void Page_Load(object sender, EventArgs e)
{
if (this.LatestLoadedControlName != "")
this.LoadUserControl(LatestLoadedControlName, Panel2);
}
protected string LatestLoadedControlName
{
get
{
return ViewState["LatestLoadedControlName"] == null ? "" : (string)ViewState["LatestLoadedControlName"];
}
set
{
ViewState["LatestLoadedControlName"] = value;
}
}
... ...
public void LoadUserControl(string controlName, Control container)
{
//先移出已有的控件
if (LatestLoadedControlName != null)
{
Control previousControl = container.FindControl(LatestLoadedControlName.Split('.')[0]);
if (previousControl != null)
{
container.Controls.Remove(previousControl);
}
}
string userControlID = controlName.Split('.')[0];
Control targetControl = container.FindControl(userControlID);
if (targetControl == null)
{
UserControl userControl = (UserControl)this.LoadControl(controlName);
userControl.ID = userControlID;
container.Controls.Add(userControl);
LatestLoadedControlName = controlName;
}
}