Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top