locked
Dynamically Swapped UserControl: EventListeners of Buttons of UserControls are not firing RRS feed

  • Question

  • User-1845357238 posted

    I have two links, User Settings, Space Settings and two UserControls, UserSettings and SpaceSettings

    What I want to do:

    • 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.
    • EventListeners of Buttons of UserControls should fired.

    So I wrote the

    SiteSettings.aspx:

        <asp:UpdatePanel runat="server">
            <ContentTemplate>
                <asp:Panel ID="Panel1" runat="server">
                    <asp:LinkButton ID="UserLink" runat="server" OnClientClick="$('#MainContent_JYM').val('User');"
                        OnCommand="SettingsLink_OnCommand" CommandName="User" Text="User Settings" />
                </asp:Panel>
                <asp:Panel ID="Panel2" runat="server">
                    <asp:LinkButton ID="SpaceLink" runat="server" OnClientClick="$('#MainContent_JYM').val('Space');"
                        OnCommand="SettingsLink_OnCommand" CommandName="Space" Text="Space Settings" />
                </asp:Panel>
                <asp:Panel ID="PlaceHolderPanel" runat="server" Style="min-height: 500px">
                    <asp:PlaceHolder runat="server" ID="SettingsPlaceholder" />
                </asp:Panel>
                <asp:HiddenField runat="server" ID="JYM" />
            </ContentTemplate>
        </asp:UpdatePanel>

    UserSettings.ascx:

        <asp:Panel runat="server">    
             <asp:Button runat="server" ID="User" Text="User" onclick="UserSettingsButton_Click" />
        </asp:Panel>

     SpaceSettings.ascx:

       <asp:Panel runat="server">    
            <asp:Button runat="server" ID="Space" Text="Space" onclick="SpaceSettingsButton_Click" />
       </asp:Panel>

    SiteSettings.ascx.cs:

       public partial class SiteSettings : Page {	
    
    		protected void Page_Load(object sender, EventArgs e) {
    			string key = JYM.Value;
    			if (key == null || key.Trim().Length == 0) {
    				AddUserSettings();
    			} else {
    				if (key.Equals("User")) {			
    					AddUserSettings();
    				} else {			
    					AddSpaceSettings();
    				}
    			}
    		}
    	
    		protected void SettingsLink_OnCommand(object sender, CommandEventArgs commandEventArgs) {	    
    			switch (commandEventArgs.CommandName) {
    				case "User":
    					AddUserSettings();
    					break;		   
    				case "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);
    		}
        }

    SpaceSettings.ascx.cs:

        public partial class SpaceSettings : Spring.Web.UI.UserControl {
    
    	protected void SpaceSettingsButton_Click(object sender, EventArgs e) {
    	    System.Diagnostics.Debug.WriteLine("SpaceSettings--------------");
    	}
        }

    UserSettings.ascx.cs:

        public partial class SpaceSettings : Spring.Web.UI.UserControl {
    
    	protected void SpaceSettingsButton_Click(object sender, EventArgs e) {
    	    System.Diagnostics.Debug.WriteLine("UserSettings--------------");
    	}
        }

    Now everything is fine, excpet the EventListeners correspond to the User button of UserSettings and Space button of SpaceSettings, are fireing on seond time and so on clicking.

    How can I solve this problem?

    Thanks.

    Sunday, April 15, 2012 9:42 AM

Answers

  • User-1845357238 posted

    I found the soultion. The problem is the ID of UserControlbeing added. I have modified two methods AddUserSettings and AddSpaceSettings in the following way:

    private void AddUserSettings() {
    	UserSettings settings = LoadControl("~/UserControls/UserSettings.ascx") as UserSettings;
    	settings.ID = "usersetting";
    	AddSettings(settings);
    }
    
    private void AddSpaceSettings() {
    	SpaceSettings settings = LoadControl("~/UserControls/SpaceSettings.ascx") as SpaceSettings;
    	settings.ID = "spacesettings";
    	AddSettings(settings);
    }

    Now it is working.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 15, 2012 10:40 AM

All replies

  • User-1845357238 posted

    I found the soultion. The problem is the ID of UserControlbeing added. I have modified two methods AddUserSettings and AddSpaceSettings in the following way:

    private void AddUserSettings() {
    	UserSettings settings = LoadControl("~/UserControls/UserSettings.ascx") as UserSettings;
    	settings.ID = "usersetting";
    	AddSettings(settings);
    }
    
    private void AddSpaceSettings() {
    	SpaceSettings settings = LoadControl("~/UserControls/SpaceSettings.ascx") as SpaceSettings;
    	settings.ID = "spacesettings";
    	AddSettings(settings);
    }

    Now it is working.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 15, 2012 10:40 AM
  • User3866881 posted

    Congratulations!Welcome to our forum next time to share more solutions and talk with us about the technology of ASP.NET……

    Monday, April 16, 2012 9:45 PM