Home All Groups Group Topic Archive Search About
Author
24 Sep 2007 7:12 AM
Lamis
Hi,
I just started with .Net 2.0 and have created a few user controls that I
would like now to use in my webpage. I need to create them in my onload and
add them into a table on the aspx page. I am using C#. My proble is that I
get exception "Use the "new" keyword to create an object of instance" "Check
to determin if the object is null before calling the method".
It seems to me that InitializeComponents() method has not executed and all
labels, buttons and dropdown list in my component has value null..
Any Idea how to create instance of my components dynamically and show them
in a aspx page???

--
LZ

Author
24 Sep 2007 7:30 PM
Teemu Keiski
Hi,

you basically cannot instantiate user control with new keyword, since that
would instantiate only UC's code-behind class if any. The markup side would
not be parsed and associated with the instance.

Instead, use

Control c = Page.LoadControl("control.ascx");


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

Show quote
"Lamis" <La***@discussions.microsoft.com> wrote in message
news:3A8AC869-64DF-42EC-8912-031B1220354E@microsoft.com...
> Hi,
> I just started with .Net 2.0 and have created a few user controls that I
> would like now to use in my webpage. I need to create them in my onload
> and
> add them into a table on the aspx page. I am using C#. My proble is that I
> get exception "Use the "new" keyword to create an object of instance"
> "Check
> to determin if the object is null before calling the method".
> It seems to me that InitializeComponents() method has not executed and all
> labels, buttons and dropdown list in my component has value null..
> Any Idea how to create instance of my components dynamically and show them
> in a aspx page???
>
> --
> LZ

AddThis Social Bookmark Button