This is a similar question, but I am facing trouble to implement the solution shown there.
In my case I have two links, User Settings, Space Settings and two UserControls, viz, UserSettings.ascx
, SpaceSettings.ascx
.
- On first time when user will visit the page he will see the User Settings.
- Depending on which one of these two links has been clicked, the PlaceHolder will swap UserControl.
What I have done is shown below:
public partial class SiteSettings : RefreshDitectedPage {
protected void Page_Load(object sender, EventArgs e) {
}
protected override void OnInit(EventArgs e) {
base.OnInit(e);
string key = Session["Settings"] as String;
System.Diagnostics.Debug.WriteLine(key);
if (key == null) {
Session["Settings"] = "User";
AddUserSettings();
} else {
if (key.Equals("User")) {
AddUserSettings();
} else {
AddSpaceSettings();
}
}
}
protected void SettingsLink_OnCommand(object sender, CommandEventArgs commandEventArgs) {
switch (commandEventArgs.CommandName) {
case "User":
Session["Settings"] = "User";
AddUserSettings();
break;
case "Space":
Session["Settings"] = "Space";
AddSpaceSettings();
break;
}
}
private void AddUserSettings() {
AddSettings(LoadControl("~/UserControls/UserSettings.ascx") as UserSettings);
}
private void AddSpaceSettings() {
AddSettings(LoadControl("~/UserControls/SpaceSettings.ascx") as SpaceSettings);
}
private void AddSettings(UserControl control) {
SettingsPlaceholder.Controls.Clear();
SettingsPlaceholder.Controls.Add(control);
}
/*protected void Page_Unload(object sender, EventArgs e) {
Session["Settings"] = null;
System.Diagnostics.Debug.WriteLine("Unload called");
}*/
}
This is not working. The results are:
- On first time visit the Session["Settings"] is null and the UserControl
UserSettings.ascx
has been loaded(Right) and Log fromPage_Load
method ofUserSettings.ascx.cs
has been displayed in Output window(Right). - When the Space Settings link has been clicked, the UserControl
SpaceSettings.ascx
has been loaded(Right) but in Output window it is printing User (Wrong) also the Logs from thePage_Load
methods of bothUserSettings.ascx.cs
andSpaceSettings.ascx.cs
have been displayed(UserSettings.ascx.cs
's Log is wrong). - When the User Settings link has been clicked, the UserControl
UserSettings.ascx
has been loaded(Right) but in Output window it is printing Space (Wrong) also the Logs from thePage_Load
methods of bothUserSettings.ascx.cs
andSpaceSettings.ascx.cs
have been displayed(SpaceSettings.ascx.cs
's Log is wrong). - I have added two Buttons to each one of the UserControls and when, say, the Button of
SpaceSettings.ascx
has been clicked it is not firing the Event in backing class first time, but it does if it is being clicked for the second time. Fist time it is Logging User (Wrong) and second time Space (Right) then the event is going to fired. - When I visit the page second time I am seeing the control, which was loaded last. Since the
Session["Settings"]
is not null then. I have tried to set this value to null inPage_Unload
and inOnUnload
methods but both of them are not working.Page_Unload
is breaking the system, since it is being called every time whilePostBack
andOnUnload
is not being called atall.
I have tried to use Request["Settings"]
instead of Session["Settings"]
, but Request["Settings"] = "User"
and Request["Settings"] = "Space"
are giving me compile time error:
Property or indexer 'System.Web.HttpRequest.this[string]' cannot be assigned to
How can I solve this?
Thanks.