すべてのページの背後にMasterPageがあり、次にUserControlsを動的にロードするメニューのあるページが作成されたサイトを作成しました。その中に、他のUserControlsを動的にロードするメニューのあるUserControlがあります。
私の問題は次のとおりです。メニューを持つUserControlは、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();
}