Reputation:
There is a master page with PlaceHolder on it. When a menu item is selected, it dynamically loads a user control ascx.
PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add("pages/MyControl.ascx");
But there's a button on MyControl, and there's a record in its source
<asp:Button ID="reg" runat="server" OnClick="reg_Click" Text="Reg" Width="207px" />
Also, the callback is in codebehind for MyControl.
public partial class RegInit : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void reg_Click(object sender, EventArgs e)
{
// never comes here
}
}
But the reg_Click is never called.. what I see, is just the reload of the master page, when "Reg" button's clicked.
Upvotes: 0
Views: 167
Reputation:
I have to publish my solution as well.
As to me, I think I found the better solution.
The idea of the user control is to be able to place many elementary controls on the panel and use them as one, and the second - the values in the elementary controls should be kept, whatever user enters, while he switches the set of user controls.
Now the solution. I just created all of my controls, but instead of using PlaceHolder, I explicitly put them on the same place of the Main Page, but with the property "Visible" set to False. Then, as the menu item is selected, I just set the property Visible of the appropriate control to True. Therefore, it works as should to..
To place a Control of the page use the tag Register: In my example there are 2 controls About and RegInit in MainPage.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="MyApp.MainPage" %>
<%@ Register TagPrefix="uc" TagName="About" Src="~/controls/About.ascx" %>
<%@ Register TagPrefix="uc" TagName="RegInit" Src="~/controls/RegInit.ascx" %>
And below on their's place in code:
<uc:About id="about" runat="server" Visible="true" />
<uc:RegInit id="regInit" runat="server" Visible="false" />
And in MainPage.aspx.cs
protected void MainMenu_ItemClick(object sender, MenuEventArgs e)
{
if (e.Item.Text == "ABOUT")
{
about.Visible = true;
regInit.Visible = false;
}
if (e.Item.Text == "REGISTER")
{
regInit.Visible = true;
about.Visible = false;
}
}
It is much better, because all the user data which are entered is kept, and user could continue with them by switching the controls.
I will tick the Connor's solution, but I decided to get rid of the PlaceHolder...
Upvotes: 1
Reputation: 73761
After the menu command has been selected, the user control must be loaded in the PlaceHolder on every postback. You can remember in a Session variable if the menu command has been selected.
// Property giving the show/hide status of the user control
private bool ShouldLoadMyControl
{
get { return Session["LoadMyControl"] != null && (bool)Session["LoadMyControl"]; }
set { Session["LoadMyControl"] = value; }
}
// Load the control on every postback (if option selected)
protected void Page_Load(object sender, EventArgs e)
{
if (ShouldLoadMyControl)
{
LoadMyControl();
}
}
// Utility function to load the user control
private void LoadMyControl()
{
PlaceHolder1.Controls.Add(LoadControl("pages/MyControl.ascx"));
}
// Event handler for the command to show the control
protected void mnuLoadMyControl_Click(object sender, EventArgs e)
{
if (!ShouldLoadMyControl)
{
ShouldLoadMyControl= true;
LoadMyControl();
}
}
// Event handler for a command to hide the control
protected void mnuHideMyControl_Click(object sender, EventArgs e)
{
ShouldLoadMyControl= false;
PlaceHolder1.Controls.Clear();
}
Upvotes: 0