I am new to asp.net and experimenting with it to find out the life cycle of a page. Here is a problem that I have not been able to solve in the last few days.
I have a hosting page (.aspx). Then I have two user controls (.ascx). The page has a place control in which it loads user controls one at a time, depending on the flow of the application. The first user control is loaded when the application starts. It has a continue button. Click Continue to download the second user control, which has two buttons - Back and Submit. Obviously, the back button should load the first user control again, and the submit button should send the form data. Pretty simple.
The problem is that the command button event handler that I have in the second user control does not fire the first time. (I have one event handler for both buttons). The user control load event fires, but then the click of the button is ignored. If I click on him again, he will shoot. I reload the controls on the page to each page_load. Here is some relevant code:
AddPlayer.aspx:
protected void Page_Load (object sender, EventArgs e) {PlaceHolder1.Controls.Clear ();
Control ctlToAdd = LoadControl("ctlInputPlayer.ascx", null);
if (ctlToAdd != null)
{
_ctlInputPlayer = (ctlInputPlayer)ctlToAdd;
_ctlInputPlayer.SendPlayerData += new EventHandler(ctlInputPlayer_SendPlayerData);
PlaceHolder1.Controls.Add(_ctlInputPlayer);
}
PlayerData player = (PlayerData)ViewState["Player"];
if (player != null)
{
ctlToAdd = LoadControl("ctlPlayerInfo.ascx", player);
if (ctlToAdd != null)
{
_ctlPlayerInfo = (ctlPlayerInfo)ctlToAdd;
_ctlPlayerInfo.SubmitPlayerData += new EventHandler(ctlPlayerInfo_SubmitPlayerData);
PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(_ctlPlayerInfo);
}
}
}
ctlPlayerInfo.ascx (second user control):
ctlPlayerInfo.ascx.cs
protected void CommandBtn_Click (object sender, CommandEventArgs e) {switch (e.CommandName) {case "Send":
break;
case "Back":
break;
}
}
protected void Page_Load ( , EventArgs e) {
// ....
}
_Load , "CommandBtn_Click" . , . , .
. , . !