Problem with createchildcontrols in an asp.net 2.0 web part

Last post 10-04-2006 7:09 AM by BCdotNET. 6 replies.

Sort Posts:

  • Problem with createchildcontrols in an asp.net 2.0 web part

    10-03-2006, 7:36 AM
    • Loading...
    • umantha
    • Joined on 07-19-2006, 9:09 AM
    • Hyderabad
    • Posts 3

    Hi,

     I have the following Scenario:

     1. A composite Control called ctrl1.

    2. In order to add it to a webpartzone at runtime i have written a wrapper class. This wrapper class inherits from the webpart calss and creates an instance of ctrl1 and adds it to the controls collection in the "CreateChildControls()" method.

    3. On the aspx page onInit i am calling the webPartmanager.AddwebPartmethod and in this i am adding an instance of the wrapper class.

    4. All this is enabling me to add the composite control to the web part zone at runtime.

    5. However when i click on my composite control despite having a postback i am still seeing a new instance of ctrl1 being created and added to this web part. This is NOT the expected behaviour for me.

     Please suggest how i can resolve this. I have tried overriding EnsureChildControls() but it doesnt help much.

    Please advice.

    Thanks and Regards

    Uma

    Thanks and Regards
    Uma
  • Re: Problem with createchildcontrols in an asp.net 2.0 web part

    10-03-2006, 11:38 PM

    On add event did you tried this:

    if (WebPartManager1.WebParts[Name_Of_Webpart]!=null)
    {
        //add code
    }

    /*
    else
    {//do nothing}
    */

     

    I compete with myself to motivate me!!

    Do not forget to mark posts, that help you, as "Answer".
  • Re: Problem with createchildcontrols in an asp.net 2.0 web part

    10-03-2006, 11:39 PM
    oops

    Sorry I mistyped the 1st line

    if (WebPartManager1.WebParts[Name_Of_Webpart]==null)
    //add code
    I compete with myself to motivate me!!

    Do not forget to mark posts, that help you, as "Answer".
  • Re: Problem with createchildcontrols in an asp.net 2.0 web part

    10-04-2006, 1:47 AM
    • Loading...
    • umantha
    • Joined on 07-19-2006, 9:09 AM
    • Hyderabad
    • Posts 3

    Hi,

     Sorry, whatever you suggested doesn't work.

     The reason being that The webpart manager does not personalize its contents in a GET method.

     So when a postback occurs its contents are empty. So comparing for "name of web part" or for "webparts.count" etc will all fail.

    Shall publish the solution when i find it. I think i would need to override the webPartManager class and write a custom manager.

    Am working on it and shall confirm once it works.

    Thanks and Regards
    Uma
  • Re: Problem with createchildcontrols in an asp.net 2.0 web part

    10-04-2006, 1:55 AM

    You are correct. I tried it but it doesn't work.

    However in my project (Same requirement as of yours) I solved it as follows:


    public static bool IsWebpartInMemory(WebPartManager WPM, string TitleOfControlToBeAdded)
    {
    foreach(WebPart w in WPM.Webparts)
      (if w.Title==Title)
       return true; //Yes it already exists...So dont add
    else
      return false; //No it doesnot exists. So add it.

    }

    If you find a better solution, do post it please.

    I compete with myself to motivate me!!

    Do not forget to mark posts, that help you, as "Answer".
  • Re: Problem with createchildcontrols in an asp.net 2.0 web part

    10-04-2006, 4:46 AM
    • Loading...
    • umantha
    • Joined on 07-19-2006, 9:09 AM
    • Hyderabad
    • Posts 3

    Hi,

     The solution to this problem is to write a custom web part manager.

     In that have a public method called SetDirty().

    This must internally call the SetPersonalizationDirty and that will solve the problem.

     Also ensure that u are checking for postback or not inorder to add to the web part.

    This has worked for me.

    Thanks and Regards
    Uma
  • Re: Problem with createchildcontrols in an asp.net 2.0 web part

    10-04-2006, 7:09 AM
    • Loading...
    • BCdotNET
    • Joined on 09-05-2006, 9:46 AM
    • Posts 38
    Here's how we solved something similar : http://forums.asp.net/thread/1401652.aspx
    Life would be a lot easier if we could take a look at the source code.
Page 1 of 1 (7 items)
Microsoft Communities
Page view counter