2012-04-14 74 views
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); 
     } 
} 

回答

2

在您的WebForm中創建一個Property,如下所示。

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

現在您可以訪問UserSettingsControl的內存。因爲它將持續在Postback。在原始代碼中,UserSettingsControlPostBack之間被重置爲空。


通過在運行時 創建的Page Life Cycle所有控件端將佈置。最後,在Postback之後,您無法找到在 運行時創建的控件。只有每個PostBack需要 娛樂相同的控件。

+0

此過程根本不添加'UserSettingsControl'。 – 2012-04-14 13:30:25

+0

什麼是錯誤來了? – Pankaj 2012-04-14 13:37:10

+0

在添加I日誌UserSettingsControl(它正在打印對象)之前,我還沒有收到任何錯誤。所以我沒有發現錯誤。 – 2012-04-14 13:44:15

2

你可以只是沒有使用佔位符,並有有控制的全部時間。然後,linkBut​​ton可以切換控件的可見性。

主要問題是您將控件添加到頁面linkBut​​ton單擊。當添加到Page_Init和Page_PreInit中時,動態添加的控件效果最佳,這使得它們可以保持其ViewState。還必須在每次回發中將其添加到佔位符。如果在您的示例中,另一個控件在將SettingsControl添加到佔位符後導致回發,則SettingsControl將消失,因爲它不會在每個回發中添加。

相關問題