[RESOLVED]Error Retrieving current

I am working an example where I am using a web user control to define a webpart.  However, when I try to get the current webpart manager I get the following error:

Error 2 An object reference is required for the nonstatic field, method, or property ‘System.Web.UI.Control.Page.get’ C:UsersTom MagaroDocumentsVisual Studio 2005WebSitesABC_IncWebPartsSimpleDisplayModeMenu.ascx.cs 14 66 C:…ABC_Inc

Here is my code

public partial class WebParts_DisplayModeMenu : System.Web.UI.UserControl

 {

    WebPartManager WPM = WebPartManager.GetCurrentWebPartManager(Page);

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    void Page_Init(object sender, EventArgs e)

    {

    }

    void Page_InitComplete(object sender, EventArgs e)

    {        

     string modeName;

     ListItem ddlModeItem;

     foreach(WebPartDisplayMode displayMode in WPM.SupportedDisplayModes)

     {

        modeName = displayMode.Name;

        if (displayMode.IsEnabled(WPM))

       {

         ddlModeItem = new ListItem(modeName, modeName);

         ddlMode.Items.Add(ddlModeItem);

       }

      }

      if (WPM.Personalization.CanEnterSharedScope)        

     {  

       pnlPersonalizeMode.Visible = true;

       if (WPM.Personalization.Scope == PersonalizationScope.User)  

      {

        rdbUserScope.Checked = true;

      }

      else

      {

        rdbSharedScope.Checked = true;

      }

     }

    }

    protected void ddlMode_SelectedIndexChanged(object sender, EventArgs e)    

   {

    string selMode;

    WebPartDisplayMode displayMode;

    selMode = ddlMode.SelectedValue;

    displayMode = WPM.SupportedDisplayModes[selMode];

    if (!(displayMode == null))

    {

     WPM.DisplayMode = displayMode;      

    }  

   }

}

 

The example is written in VB but I have transposed it to C#.  Is the Page object correct?

flashman

 WebPartManager WPM = WebPartManager.GetCurrentWebPartManager(Page);

 

Is ‘Page" name of your page? Basically it expects tha Web page that contains an instance of the WebPartManager.

No, that is just what was given in the example.  How would I retrieve the name of the containing page?

Try this

public partial class WebParts_DisplayModeMenu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            WebPartManager WPM = WebPartManager.GetCurrentWebPartManager(this.Parent.Page);
        }
    }

Hey Bimalvv,

 

Thanks for the info.  The example didn’t no have the statement in the load event.

It worked thanks.

 

Leave a Reply