Why is the Page property in my web part null?

  • Why is the Page property in my web part null? paul

    I am trying to include an Image control within my web part. In EnsureChildControls I have the following code:

    Image image = new Image();
    image.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "hotline.jpg");

    I get a NullPointer exception and this is because 'Page' is null.

    Why might this be? Am I doing this all wrong anyway?

  • Are you calling the parent EnsureChildControls() at the start of your overridden method?

    Also, have you tried your code in CreateChildControls()?

  • Can you give us some more details on how you run this Web Part, for instance in which context?

    How do you call EnsureChildControls and why are you adding the image there and not in CreateChildControls?

  • Be sure not to call EnsureChildControls() from within the constructor of your WebPart.

    Also, I would not override EnsureChildControls but override CreateChildControls instead, as James and Wictor have already mentioned in their answers.

Related questions and answers
  • the following tags on my test: [TestMethod()] [HostType("ASP.Net")] [UrlToTest("http://localhost/SitePages/Home.aspx")] Any idea why I am getting this error? ...I have some code that creates a SharePoint site. When the code runs as part of the solution it works. However, when I run it as a unit test I get the following error: The test adapter 'WebHostAdapter' threw an exception while running test 'CreateSiteTest'. The web site could not be configured correctly; getting ASP.NET process information failed. Requesting 'http://localhost

  • ["Doc"]; view.ListId = list.ID.ToString(); view.ViewId = list.Views["ReadListByCategory"].ID.ToString(); Controls.Add(view); } After adding web part on page i got error: !--Unable to display web part--. There is no problem with other sharepoint lists. Only with external list. Does anyone have experience in creating Web Part with External list in it? ...Using VS 2010 i've created BCS model for external MS SQL source, deployed it with success, then created external list - it works perfect. Now my task is - create visual web part in VS2010 and place

  • The following code runs fine as an administrator but crashes with a 403 forbidden error when run as a non-admin on the am.GetAudience("My Audience") call. Can anyone spot why, I'm running with elevated privileges? List<string> accounts = new List<string>(); SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite("http://mysite")) { AudienceManager am = new AudienceManager(ServerContext.GetContext(site)); foreach (UserInfo member in am.GetAudience("My Audience").GetMembership()) { accounts.Add

  • and tested for providing a value via a connection. Is there anything that explains why I'm unable to set up a connection? Here is the code for my web part: public class ProgramEntityTokenConverter..., perform some processing, then spit out a 'token' for consumption by a different connected web part. The token is just the ID in another format. I have the drop-down menu correctly displaying as follows: Connections -> Send Program Entity Token To -> ... -> Get Program Entity ID From -> ... When I select a web part in "Get Program Entity ID From", a dialog appears for me to choose

  • part NOT a page. Also, it is not possible to oblige the clients to change their forms from POST to GET just to use my little web part (I think you understand me). I put a breakpoint inside the web...My problem precisely and concisely is that I developed a web part which contains a FileUpload control and the web part in general works correctly except for one issue. Whenever I use the web part...-scope is only within the web part NOT the full page. Also, I do not want to use any AJAX-controls. Update: I was told that the only way to get rid of "Webpage has expired" error is either by: Using

  • For all the Web Parts on my SP 2010 team site wiki page, the "Hidden" option is greyed out (under Edit Web Part > Layout). How can I activate this option?

  • I have been trying to get the organization browser web part to work on a publishing site and I can't get the web part to display any users. When I look at the organization browser web part on the my site pages, it works as expected. I think the same profile service is being used by the publishing site (I only have one profile service) but I am not sure how to verify this. Does this web part only work on My Site pages?

  • I'm trying to add a custom web part (that is one I have written myself) using the new Javascipt OM available with SharePoint 2010. My solution is also running within the Sandbox. I have it working correctly for the Content Editor Web Part (based on sample code), however when I replace the Content Editor Web Part definition with one for my own web part I get the following error... --------------------------- Things I have tested: I can successfully upload my .webpart definition to the web part gallery, and the web part works. I can successfully add the web part to the page

  • I'm writing some code against the search API in SharePoint 2007, and am seeing some interesting behaviour. I'm using the KeywordQuery 'model' rather than FullTextSqlQuery, since this matches the advanced search form our client wants. I'm using the following code to execute the search - this builds a keyword search query in the same format as the OOTB web parts: ResultTableCollection resultTables = null; using (KeywordQuery keywordQuery = new KeywordQuery(SPContext.Current.Site)) { string fullQuery = string.Empty; // TODO: refactor