0

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 from Page_Load method of UserSettings.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 the Page_Load methods of both UserSettings.ascx.cs and SpaceSettings.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 the Page_Load methods of both UserSettings.ascx.cs and SpaceSettings.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 in Page_Unload and in OnUnload methods but both of them are not working. Page_Unload is breaking the system, since it is being called every time while PostBack and OnUnload 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.

Community
  • 1
  • 1
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • You might wait for others to comment on how to resolve it, but isn't it time to switch to jQuery for such client side tabs? http://docs.jquery.com/UI/Tabs – Lex Li Apr 15 '12 at 11:54
  • @Lex Li why should I use jQuery tab in this case? I guess then I have to load all the contents on page load and that would be very bad. – Tapas Bose Apr 15 '12 at 14:49
  • jQuery tabs supports AJAX, too. – Lex Li Apr 16 '12 at 01:33

1 Answers1

0

I had tried a different approach to solve my requirement, and that solves the swapping of UserControl but then I encountered that the EventHandlers of the Buttons of the UserControls were not being fired first time, but were firing if it is being clicked twice.

I asked this question in ASP.NET forum. But there nobody answered to my question. But I was trying die hard to find the solution and atlast I made it possible. The problem was in the ID. I had posted the solution in that forum.

Here is the link. This might be useful for future users.

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331