Search code examples
sharepoint-2010web-parts

Is it feasible to dynamically assign a VisualWebPart instance to a WebPart?


I am working on a WebPart that needs to morph based on the current user; if one person, they will see a certain collection of controls, another person will see something else (based on their role/the situation).

Is a sensible way of accomplishing this to create N VisualWebParts, and then swap out the specific VWP hosted by the WebPart based on the current user? Or is there a standard way of doing this that is better/easier (I am new to Sharepoint, hence I don't know what's "normal").


Solution

  • I think there are many ways to dynamically change a webpart's contents. One way to accomplish this in a single VisualWebPart is to have multiple sections of controls each encapsulated by a panel control. Control the visibility of the panels using code to create your dynamic rendering.

      <asp:Panel id="DefaultPanel" runat="server" Visible="True">
      <h3>Default Text or controls here</h3></asp:Panel> 
    
        <asp:Panel id="SpecialPanel" runat="server" Visible="false">
    <h3>Special Text or controls</h3></asp:Panel>
    

    Then in code

    bool showSpecialTextOnly = (someBooleanTestCondition);
    if (showSpecialTextOnly )
    {
       DefaultPanel.Visible=false;
       SpecialPanel.Visible=true;
    }