2

我想要实现的是,如果用户控件已经添加到占位符,那么它将被删除,否则将被添加到它,它将在 LinkBut​​ton 的 onclick 中完成。

编码:

public partial class SiteSettings : System.Web.UI.Page {
    private UserSettings UserSettingsControl;
    protected void Page_Load(object sender, EventArgs e) {
        System.Diagnostics.Debug.WriteLine("Pageload");
        UserSettingsControl = LoadControl("~/UserControls/UserSettings.ascx") as UserSettings;
    }

    protected void UserLink_Click(object sender, EventArgs e) {     
        if (SettingsPlaceholder.Controls.Contains(UserSettingsControl)) {
        System.Diagnostics.Debug.WriteLine("Contains");
        SettingsPlaceholder.Controls.Remove(UserSettingsControl);
        } else {
        System.Diagnostics.Debug.WriteLine("Does not Contains");
        SettingsPlaceholder.Controls.Add(UserSettingsControl);
        }
    }
}

现在它不工作了。我得到:

Pageload // on first time load
Pageload // on first time click
Does not Contains // on first time click
Pageload // on second time click
Does not Contains // on second time click

在输出窗口中。

我怎样才能做到这一点?我也尝试将它存储到 ViewState 中,但由于 UserControl 不可序列化,所以它不起作用。

aspx 页面是:

<telerik:RadAjaxManager ID="AjaxManager" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="UserLink">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="SettingsPanel" LoadingPanelID="LoadingPanel" UpdatePanelRenderMode="Block" />
                <telerik:AjaxUpdatedControl ControlID="PlaceHolderPanel" />
            </UpdatedControls>
        </telerik:AjaxSetting>            
    </AjaxSettings>
    <ClientEvents OnResponseEnd="respondEnd" />
</telerik:RadAjaxManager>
<asp:Panel ID="SettingsPanel" runat="server">
    <telerik:RadSplitter ID="MainSplitter" runat="server" MinHeight="200" Width="100%"
        OnClientLoaded="splitterLoaded" OnClientResized="splitterLoaded">
        <telerik:RadPane ID="LeftPane" runat="server" MaxWidth="250" Width="150" MinWidth="150" CssClass="left-rounded-corner settings-splitter-left">
            <asp:Panel runat="server">
                <asp:LinkButton ID="UserLink" runat="server" onclick="UserLink_Click" Text="User Settings" />                    
            </asp:Panel>
        </telerik:RadPane>
        <telerik:RadSplitBar ID="Splitbar" runat="server" CollapseMode="Forward" />
        <telerik:RadPane ID="RightPane" runat="server" CssClass="right-rounded-corner settings-splitter-right">
            <asp:Panel ID="PlaceHolderPanel" runat="server" Height="100%">
                <asp:PlaceHolder runat="server" ID="SettingsPlaceholder" />
            </asp:Panel>
        </telerik:RadPane>
    </telerik:RadSplitter>
</asp:Panel>
<telerik:RadAjaxLoadingPanel ID="LoadingPanel" runat="server" />

编辑:

修改后的代码:

public partial class SiteSettings : System.Web.UI.Page {

            protected void Page_Load(object sender, EventArgs e) {
                    if (!IsPostBack) {
                        AddUserSettings();
                    }
            }

            public UserControl UserSettingsControl {
                    get {
                            if (ViewState["UserSettings"] == null) {
                                    ViewState["UserSettings"] = LoadControl("~/UserControls/UserSettings.ascx") as UserSettings;
                            }
                            return (UserControl)ViewState["UserSettings"];
                    }
            }

        public UserControl SpaceSettingsControl {
                get {
                        if (ViewState["SpaceSettings"] == null) {
                                ViewState["SpaceSettings"] = LoadControl("~/UserControls/SpaceSettings.ascx") as SpaceSettings;
                        }
                        return (UserControl)ViewState["SpaceSettings"];
                }
        }

        protected void SettingsLink_OnCommand(object sender, CommandEventArgs commandEventArgs) {
                switch (commandEventArgs.CommandName) {
                    case "User":
                            AddUserSettings();
                            break;

                    case "Space":
                            AddSpaceSettings();
                            break;
                }
        }

        private void AddUserSettings() {
                AddSettings(UserSettingsControl);
        }

        private void AddSpaceSettings() {
                AddSettings(SpaceSettingsControl);
        }

        private void AddSettings(UserControl control) {
                SettingsPlaceholder.Controls.Add(control);
        }
}
4

2 回答 2

2

您不能只使用 PlaceHolder 并一直控制在那里。然后 linkBut​​ton 可以切换控件的可见性。

主要问题是您将控件添加到页面链接按钮单击。动态添加的控件在 Page_Init 和 Page_PreInit 中添加时效果最佳,这允许它们保持其 ViewState。此外,它们必须在每次回发时添加到占位符中。如果在您的示例中,在将 SettingsControl 添加到占位符后,另一个控件导致回发,则 SettingsControl 将消失,因为它没有在每次回发时添加。

于 2012-04-14T13:07:29.347 回答
2

Property在你WebForm喜欢的下面创建一个。

public UserSettings UserSettingsControl 
{
    get
    {
        if (Session["MyControl"] == null)
            Session["MyControl"] = 
            LoadControl("~/UserControls/UserSettings.ascx") as UserSettings;
        return (UserSettings)Session["MyControl"];
    }
}

现在您可以访问UserSettingsControl. 因为它将持续存在于Postback. 在原始代码中,UserSettingsControl被重置为 null PostBack


到最后,Page Life Cycle在运行时创建的所有控件都将被释放。最后,您找不到在运行时创建的控件 after Postback。每个 . 只需要重新创建相同的控件PostBack

于 2012-04-14T13:13:39.063 回答