我创建了一个站点,在所有页面后面都有一个 MasterPage,然后是一个带有动态加载我的用户控件的菜单的页面,在其中,一个用户控件具有一个动态加载其他用户控件的菜单...
我的问题是:有菜单的用户控件,在我第一次触发 OnClick 事件后,我无法再次触发它......有人知道为什么吗?
代码如下:
这页纸:
public partial class AboutBrazil : BasePage
{
private String LastLoadedControl_AboutBrazil
{
get
{
if (ViewState["LastLoadedControl_AboutBrazil"] != null)
{
return ViewState["LastLoadedControl_AboutBrazil"].ToString();
}
else
{
return "~/UserControl/AboutBrazil/ucIntroduction.ascx";
}
}
set
{
ViewState["LastLoadedControl_AboutBrazil"] = value;
}
}
private void LoadUserControl()
{
UserControl userControl = (UserControl)Page.LoadControl(LastLoadedControl_AboutBrazil);
phAboutBrazil.Controls.Clear();
phAboutBrazil.Controls.Add(userControl);
userControl.ID = "myUserControl";
}
protected void Page_Load(object sender, EventArgs e)
{
this.SetTitulo("About Brazil");
LoadUserControl();
}
protected void btnLoadUserControl_Click(object sender, EventArgs e)
{
System.Drawing.Color foreColorI = System.Drawing.Color.FromArgb(102, 102, 102);
System.Drawing.Color foreColorA = System.Drawing.Color.FromArgb(27, 41, 13);
Button btnSender = (Button)sender;
btnIntroduction.ForeColor = foreColorI;
btnHistory.ForeColor = foreColorI;
btnThePeople.ForeColor = foreColorI;
btnBrazilianRegions.ForeColor = foreColorI;
btnBrazilianEcosystem.ForeColor = foreColorI;
btnGeneralInformation.ForeColor = foreColorI;
btnTips.ForeColor = foreColorI;
switch (btnSender.CommandArgument)
{
case "Introduction":
btnSender.ForeColor = foreColorA;
LastLoadedControl_AboutBrazil = "~/UserControl/AboutBrazil/ucIntroduction.ascx";
break;
case "History":
btnSender.ForeColor = foreColorA;
LastLoadedControl_AboutBrazil = "~/UserControl/AboutBrazil/ucHistory.ascx";
break;
case "The People":
btnSender.ForeColor = foreColorA;
LastLoadedControl_AboutBrazil = "~/UserControl/AboutBrazil/ucThePeople.ascx";
break;
case "The Brazilian Regions":
btnSender.ForeColor = foreColorA;
LastLoadedControl_AboutBrazil = "~/UserControl/AboutBrazil/ucTheBrazilianRegions.ascx";
break;
default:
btnSender.ForeColor = foreColorA;
LastLoadedControl_AboutBrazil = "~/UserControl/AboutBrazil/ucIntroduction.ascx";
break;
}
this.LoadUserControl();
}
}
第一个用户控制:
private List<String> listControl = new List<String>(new String[] { "Introduction", "North", "Northeast", "Middle-West", "South", "Southeast" });
private String LastLoadedControl_TheBrazilianRegions
{
get
{
if (ViewState["LastLoadedControl_TheBrazilianRegions"] != null)
{
return ViewState["LastLoadedControl_TheBrazilianRegions"].ToString();
}
else
{
return "~/UserControl/AboutBrazil/TheBrazilianRegions/ucIntroduction.ascx";
}
}
set
{
ViewState["LastLoadedControl_TheBrazilianRegions"] = value;
}
}
private String LastClickedButton_TheBrazilianRegions
{
get
{
if (ViewState["LastClickedButton_TheBrazilianRegions"] != null)
{
return ViewState["LastClickedButton_TheBrazilianRegions"].ToString();
}
else
{
return "Introduction";
}
}
set
{
ViewState["LastClickedButton_TheBrazilianRegions"] = value;
}
}
private void LoadUserControl()
{
UserControl userControl = (UserControl)Page.LoadControl(LastLoadedControl_TheBrazilianRegions);
phTheBrazilianRegions.Controls.Clear();
phTheBrazilianRegions.Controls.Add(userControl);
userControl.ID = "myUserControlNeasted";
}
private void LoadClickedButton()
{
foreach (String control in this.listControl)
{
HtmlTableCell tdControl = (HtmlTableCell)this.FindControl("td" + control.Replace("-", ""));
if (control == this.LastClickedButton_TheBrazilianRegions)
{
tdControl.InnerHtml = "<div class=\"menuAtivoLeft\"></div><div class=\"menuAtivoCenter\"><div class=\"menuAtivoCenterContent\">" + this.LastClickedButton_TheBrazilianRegions + "</div></div><div class=\"menuAtivoRight\"></div>";
}
else
{
try
{
Button btnButton = new Button();
btnButton.ID = "btn" + control.Replace("-", "");
btnButton.Text = control;
btnButton.CommandArgument = control;
btnButton.CssClass = "tableContentMenuContentButton";
btnButton.Click += new EventHandler(this.btnLoadUserControl_Click);
AsyncPostBackTrigger _Trigger = new AsyncPostBackTrigger();
_Trigger.ControlID = btnButton.ID;
_Trigger.EventName = "Click";
tdControl.Controls.Clear();
tdControl.Controls.Add(btnButton);
this.upTheBrazilianRegions.Triggers.Clear();
this.upTheBrazilianRegions.Triggers.Add(_Trigger);
btnButton.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
tdControl.Dispose();
}
}
protected override void OnPreRender(EventArgs e)
{
this.LoadUserControl();
this.LoadClickedButton();
base.OnPreRender(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLoadUserControl_Click(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
switch (btnSender.CommandArgument)
{
case "Introduction":
LastLoadedControl_TheBrazilianRegions = "~/UserControl/AboutBrazil/TheBrazilianRegions/ucIntroduction.ascx";
break;
case "North":
LastLoadedControl_TheBrazilianRegions = "~/UserControl/AboutBrazil/TheBrazilianRegions/ucNorth.ascx";
break;
case "Northeast":
LastLoadedControl_TheBrazilianRegions = "~/UserControl/AboutBrazil/TheBrazilianRegions/ucNortheast.ascx";
break;
case "Middle-West":
LastLoadedControl_TheBrazilianRegions = "~/UserControl/AboutBrazil/TheBrazilianRegions/usMiddleWest.ascx";
break;
case "South":
LastLoadedControl_TheBrazilianRegions = "~/UserControl/AboutBrazil/TheBrazilianRegions/ucSouth.ascx";
break;
case "Southeast":
LastLoadedControl_TheBrazilianRegions = "~/UserControl/AboutBrazil/TheBrazilianRegions/ucSoutheast.ascx";
break;
default:
LastLoadedControl_TheBrazilianRegions = "~/UserControl/AboutBrazil/TheBrazilianRegions/ucIntroduction.ascx";
break;
}
LastClickedButton_TheBrazilianRegions = btnSender.CommandArgument;
this.LoadUserControl();
this.LoadClickedButton();
}