Dynamically adding UserControls as WebParts in code-behind: can it be done?

Rate It (1)

Last post 10-16-2006 1:49 PM by vishnubobade. 10 replies.

Sort Posts:

  • Dynamically adding UserControls as WebParts in code-behind: can it be done?

    09-05-2006, 10:12 AM
    • Loading...
    • BCdotNET
    • Joined on 09-05-2006, 1:46 PM
    • Posts 38

    My situation: we have a project where an aspx page (which uses MasterPages and also uses Atlas) needs to contain WebParts that actually are UserControls, but which UserControls are available depends on the user and associated data, so a WebPart/UserControl that is available one day, might be considered off-limits for that user the next day.

    So far, I've been unable to come up with a way to do this. Even simply adding UCs as WPs ain't working. Right now I do this in the Page_Load of the aspx page -- LoadControl, then CreateWebPart, then AddWebPart -- and while this works perfectly fine when I do this the first time, it falls apart after a postback: either the controls get added again and again on each postback, or they fail to capture the postback-event, etcetera. Ditto once the controls are saved in the Profile.

    FYI: this cannot be a "static" solution. There needs to be the possibility to simply create a new UserControl, add the name in a database table, and then allow user-access to this control via a "security" framework. However, some controls also need to be subjected to other rules, i.e. they can only be accessed on certain days etc.

    It all seems to work okay when the UCs get hardcoded in the aspx page, but that isn't a viable solution IMHO: the page would get cluttered with tags, and also we don't think it is wise to check the security settings in each UC. It would also remove the flexibility of the system we had in mind: i.e. any developer can create a UserControl, and after he/she adds it to the list of available controls (in the database), the admins can decide which user has access to the control.

    I have searched these forums, but all I encounter are the same solutions that don't work for me. I would love to know whether this is even possible, or if I'm simply doing this the wrong way.

    Life would be a lot easier if we could take a look at the source code.
  • Re: Dynamically adding UserControls as WebParts in code-behind: can it be done?

    09-05-2006, 11:02 PM
    • Loading...
    • MuteThis
    • Joined on 09-21-2003, 1:10 PM
    • Cincinnati, Ohio
    • Posts 165

    For most web controls to fire postback events they need to have their ViewState loaded.  For dynamically loaded controls this means loading the control prior to the LoadViewState event to ensure the control receives its ViewState.  This means loading the control in Page_Init.  That should be the only change you need.

    Ben
    MCAD .Net (70-315, 70-320 & 70-229)
    :One that throws dirt loses ground:
  • Re: Dynamically adding UserControls as WebParts in code-behind: can it be done?

    09-06-2006, 3:52 AM
    • Loading...
    • BCdotNET
    • Joined on 09-05-2006, 1:46 PM
    • Posts 38
    MuteThis:
    For most web controls to fire postback events they need to have their ViewState loaded.  For dynamically loaded controls this means loading the control prior to the LoadViewState event to ensure the control receives its ViewState.  This means loading the control in Page_Init.  That should be the only change you need.


    If I load them there, they get added again and again to the page on each postback.

    If I put "if (!IsPostBack)" around the code that adds the parts (in Page_Init), it fails to add them on a postback.

    If I put "if (WeBPartZone1.WebParts.Count == 0)" around the code instead of "if (!IsPostBack)", it re-adds the UCs on each postback.
    Life would be a lot easier if we could take a look at the source code.
  • Re: Dynamically adding UserControls as WebParts in code-behind: can it be done?

    09-26-2006, 4:53 AM
    • Loading...
    • arbuston
    • Joined on 09-26-2006, 8:26 AM
    • Posts 25

    Hi

    could you solve your problem

    i have the same problem.

  • Re: Dynamically adding UserControls as WebParts in code-behind: can it be done?

    09-26-2006, 11:42 AM
    • Loading...
    • Grinka
    • Joined on 06-08-2006, 4:12 PM
    • Posts 21

    BCDotNet,

    you need to call your webpartmanager's SetPersonalizationDirty() method after you load your parts. However you cannot call it directly. You need to implement your own custom web part manager that extends Microsoft.Web.UI.Controls.WebParts.WebPartManager

    In your custom webpartmanager create the following method:

    public void SetDirty()

    {

    // Invoke the protected SetPersonalizationDirty method

    SetPersonalizationDirty();

    }

     

    call this method from the page that loads the web parts dynamically:

    MyCustomManater manager =  (MyCustomManater)WebPartManager.GetCurrentWebPartManager(this.Page);

    // add parts here

    manager.SetDirty();

    On your master page, replace  "<asp:WebPartManager ></asp:WebPartManager>" with your custom webpart manager.

    Hope this helps.

  • Re: Dynamically adding UserControls as WebParts in code-behind: can it be done?

    09-27-2006, 2:41 PM
    • Loading...
    • arbuston
    • Joined on 09-26-2006, 8:26 AM
    • Posts 25

    tnx for help Smile.

    but i have a problem. i load webparts based on querystring values.

    assume that i have two webparts(webpart1, webpart2) in my application.

    when the querystring is http://mysite.com/index.aspx?pageID=1

    i should load webpart1 into page(i.e index.aspx as only page of site) and when the querystring is http://mysite.com/index.aspx?pageID=2

    i should load webpart2 into page.

    it seems that  there is no problem to loading webparts. But can i change the properties of each webpart and hold their changes for other users that visit the page?

  • Re: Dynamically adding UserControls as WebParts in code-behind: can it be done?

    09-27-2006, 4:06 PM
    • Loading...
    • Grinka
    • Joined on 06-08-2006, 4:12 PM
    • Posts 21

    >  it seems that  there is no problem to loading webparts. But can i change the properties of each webpart and hold their changes for other users that visit the page?

     

    Are you trying to show the same changes to all the users (Shared Personalization) ?

    Or you are changing the contents of the controls within a webpart and trying to persist those changes for next page loads? 

     

  • Re: Dynamically adding UserControls as WebParts in code-behind: can it be done?

    09-28-2006, 5:05 AM
    • Loading...
    • arbuston
    • Joined on 09-26-2006, 8:26 AM
    • Posts 25
    i want to use shared personalization for some webparts
  • Re: Dynamically adding UserControls as WebParts in code-behind: can it be done?

    09-28-2006, 5:33 AM

    You can also try this:-

    First add all the controls in your web respective web zones.

     Now, on page_load loop through the webparts and call CloseWebPart() method for all except the one you want to display (depending upon your querystring parameter). It might not be best of code but it will solve your problem I guess.

     

    I compete with myself to motivate me!!

    Do not forget to mark posts, that help you, as "Answer".
  • Re: Dynamically adding UserControls as WebParts in code-behind: can it be done?

    09-28-2006, 8:23 AM
    • Loading...
    • BCdotNET
    • Joined on 09-05-2006, 1:46 PM
    • Posts 38

    We have solved it. Look here: http://forums.asp.net/thread/1401652.aspx 

    Life would be a lot easier if we could take a look at the source code.
  • Re: Dynamically adding UserControls as WebParts in code-behind: can it be done?

    10-16-2006, 1:49 PM

    Hi dear .... can you please send me this full code... because im getting same problem....

    waiting for your confirmation

    thanks in advance....

    vishnu

    vishnubobade@yahoo.com

Page 1 of 1 (11 items)
Microsoft Communities
Page view counter