asp.net中动态的往一个页面中添加用户控件,当切换用户控件时报错

2024-11-07 23:45:46
有2个网友回答
网友(1):

aspx:

C# : phControl.Controls.Clear();
if (LatestLoadedControlName == "DIstribution")
{
DIstribution control = (DIstribution)LoadControl("~/SpecTool/UserControl/DIstribution.ascx");
control.SpecID = this.SpecID;
phControl.Controls.Add(control);
}
else if (LatestLoadedControlName == "Oracle Task")
{
OracleTask control = (OracleTask)LoadControl("~/SpecTool/UserControl/OracleTask.ascx");
control.SpecID = this.SpecID;
control.ProjectNumber = this.hidProjectNumber.Value;
phControl.Controls.Add(control);
}

网友(2):

动态添加的控件回发时需要手动维护viewstate,建议你换个实现方式