Object reference not set to an instance of an object

G

Guest

I am getting the above error when attempt to set the src, height and width of
an iframe in C# code.

I am using this as an attempt to dynamically alter the image displayed in
the iframe tag of an aspx page. Can someome please explain why I am getting
the error and how it can be resolved - thanks.

The code and exception are below:

Code in aspx file:

<iframe runat="server" id="image1" name="image1" src="" height=""
width=""></iframe>

C# code:
HtmlControl image1 = (HtmlControl)this.FindControl("image1");

image1.Attributes["src"] = "~/images/Test.gif";
image1.Attributes["width"] = "100%";
image1.Attributes["height"] = "100%";

Exception:

[NullReferenceException: Object reference not set to an instance of an
object.]
Environmental.Page_Load(Object sender, EventArgs e) in
c:\Inetpub\wwwroot\AcornNETSite\Environmental.aspx.cs:42
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o,
Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

Thanks again

Andrew
 
D

Dan

change to :

HtmlControl image1 = new HtmlControl();
image1 = (HtmlControl)this.FindControl("image1");

image1.Attributes["src"] = "~/images/Test.gif";
image1.Attributes["width"] = "100%";
image1.Attributes["height"] = "100%";
 
G

Guest

Thanks - however this now causes a compile error of:

Cannot create an instance of the abstract class or interface
'System.Web.UI.HtmlControls.HtmlControl

Any ideas?

Andrew

--
Andrew Mercer
-----------------


Dan said:
change to :

HtmlControl image1 = new HtmlControl();
image1 = (HtmlControl)this.FindControl("image1");

image1.Attributes["src"] = "~/images/Test.gif";
image1.Attributes["width"] = "100%";
image1.Attributes["height"] = "100%";

--
Dan
Andrew Mercer said:
I am getting the above error when attempt to set the src, height and width
of
an iframe in C# code.

I am using this as an attempt to dynamically alter the image displayed in
the iframe tag of an aspx page. Can someome please explain why I am
getting
the error and how it can be resolved - thanks.

The code and exception are below:

Code in aspx file:

<iframe runat="server" id="image1" name="image1" src="" height=""
width=""></iframe>

C# code:
HtmlControl image1 = (HtmlControl)this.FindControl("image1");

image1.Attributes["src"] = "~/images/Test.gif";
image1.Attributes["width"] = "100%";
image1.Attributes["height"] = "100%";

Exception:

[NullReferenceException: Object reference not set to an instance of an
object.]
Environmental.Page_Load(Object sender, EventArgs e) in
c:\Inetpub\wwwroot\AcornNETSite\Environmental.aspx.cs:42
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o,
Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

Thanks again

Andrew
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Dan said:
change to :

HtmlControl image1 = new HtmlControl();
image1 = (HtmlControl)this.FindControl("image1");

and how this solve the OP problem?
if FindControl does not find the control being searched it will return null

It's the very same situation than the OP has :)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


The easiest way is to declare it server side:

protected System.Web.UI.HtmlControls.HtmlGenericControl image1;


you will not have need to do a findcontrol
 
G

Guest

Thanks,

Found that I did not need to declare after all - the iframe tag
was already set to 'runat="server"' so I had access to the properties in C#.

Adding the extra declaration threw an error as it was saying the item
was already declared.

However despite setting the src/url/ etc it still did not allow me to
display a .gif when either setting the values in C# or hardcoding in the aspx
file.

My ultimatre goal is to display PDF's and .DOT files within a
ContentPlaceHolder
and this was an attempt to understand the .NET img/iframe/asp:Image concepts.

Does anyone know whether this is the correct way - having searched on the
NET for some time I have not found an answer.

I can load these items into a new Browser window but only get the 'red
cross' [missing image] when trying to load into a ContentPlaceHolder.

Any suggestions greatly appreciated...

Thanks again

Andrew
 
G

Guest

Thanks for all your help - have now got pdf's and .dot and .gif all loading
nicely
into the src parameter of an iframe tag.

Seemed to work once I embedded it into a table..., was in a div

Andrew

--
Andrew Mercer
-----------------


Andrew Mercer said:
Thanks,

Found that I did not need to declare after all - the iframe tag
was already set to 'runat="server"' so I had access to the properties in C#.

Adding the extra declaration threw an error as it was saying the item
was already declared.

However despite setting the src/url/ etc it still did not allow me to
display a .gif when either setting the values in C# or hardcoding in the aspx
file.

My ultimatre goal is to display PDF's and .DOT files within a
ContentPlaceHolder
and this was an attempt to understand the .NET img/iframe/asp:Image concepts.

Does anyone know whether this is the correct way - having searched on the
NET for some time I have not found an answer.

I can load these items into a new Browser window but only get the 'red
cross' [missing image] when trying to load into a ContentPlaceHolder.

Any suggestions greatly appreciated...

Thanks again

Andrew


--
Andrew Mercer
-----------------


Ignacio Machin ( .NET/ C# MVP ) said:
Hi,


The easiest way is to declare it server side:

protected System.Web.UI.HtmlControls.HtmlGenericControl image1;


you will not have need to do a findcontrol
 
D

Dan

Sorry my fault for taking 2 seconds to read the problem, saw the error
thrown and saw he hadn't created a memory space and presumed that was it. No
need to be sarcastic i am sure you are not right all the time my good
friend. i will concede this one though :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top