سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى sharepoint.stackexchange
scroll top