Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
add comment

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

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

share|improve this answer
add comment

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?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.