I create a UserControl called "RegInfo.ascx",
then i add a PlaceHolder to WebForm1,
and in WebForm1.Page_Load() i wrote the follow statement:
placeHolder.Controls.Add(new RegInfo());
but when i pressed F5 to run the web app, it shows nothing. :(
after search web to find out solution, i replaced upper statement with:
placeHolder.Controls.Add(Page.LoadControl("RegInfo.ascx"));
it work as my expect!
I am so confused about the two results,
Don't they add a new instance of RegInfo into the placeHolder?
Why one can work exactly while anthor don't?When you load a control, you do more than instantiate it..
new RegInfo();, as you know, creates a new instance of a class.
Page.LoadControl also creates a new instance, but also does a lot
more...such as bringing the control up to the same event life cycle. AS you
can imagine, if new did this, it would be inconsitent with how new works for
normal classes (simply calling the constructor...).
Cheers,
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"alecyy" <cityyokel@.163.com> wrote in message
news:OX$bJQMQFHA.1884@.TK2MSFTNGP15.phx.gbl...
> hi,
> I create a UserControl called "RegInfo.ascx",
> then i add a PlaceHolder to WebForm1,
> and in WebForm1.Page_Load() i wrote the follow statement:
> placeHolder.Controls.Add(new RegInfo());
> but when i pressed F5 to run the web app, it shows nothing. :(
> after search web to find out solution, i replaced upper statement with:
> placeHolder.Controls.Add(Page.LoadControl("RegInfo.ascx"));
> it work as my expect!
> I am so confused about the two results,
> Don't they add a new instance of RegInfo into the placeHolder?
> Why one can work exactly while anthor don't?
when you do new RegInfo() you are only creating an instance of the code
behind of your user control, which normally doesn't have any code to create
the controls it contains (these are usually on the aspx page that get
compiled). Page.LoadControl("RegInfo.ascx") creates an instance of the
actual user control (which inhertis from the code behind).
-- bruce (sqlwork.com)
"alecyy" <cityyokel@.163.com> wrote in message
news:OX$bJQMQFHA.1884@.TK2MSFTNGP15.phx.gbl...
> hi,
> I create a UserControl called "RegInfo.ascx",
> then i add a PlaceHolder to WebForm1,
> and in WebForm1.Page_Load() i wrote the follow statement:
> placeHolder.Controls.Add(new RegInfo());
> but when i pressed F5 to run the web app, it shows nothing. :(
> after search web to find out solution, i replaced upper statement with:
> placeHolder.Controls.Add(Page.LoadControl("RegInfo.ascx"));
> it work as my expect!
> I am so confused about the two results,
> Don't they add a new instance of RegInfo into the placeHolder?
> Why one can work exactly while anthor don't?
when you do new RegInfo() you are only creating an instance of the code
behind of your user control, which normally doesn't have any code to create
the controls it contains (these are usually on the aspx page that get
compiled). Page.LoadControl("RegInfo.ascx") creates an instance of the
actual user control (which inhertis from the code behind).
-- bruce (sqlwork.com)
"alecyy" <cityyokel@.163.com> wrote in message
news:OX$bJQMQFHA.1884@.TK2MSFTNGP15.phx.gbl...
> hi,
> I create a UserControl called "RegInfo.ascx",
> then i add a PlaceHolder to WebForm1,
> and in WebForm1.Page_Load() i wrote the follow statement:
> placeHolder.Controls.Add(new RegInfo());
> but when i pressed F5 to run the web app, it shows nothing. :(
> after search web to find out solution, i replaced upper statement with:
> placeHolder.Controls.Add(Page.LoadControl("RegInfo.ascx"));
> it work as my expect!
> I am so confused about the two results,
> Don't they add a new instance of RegInfo into the placeHolder?
> Why one can work exactly while anthor don't?
thx.
i got it.
"Bruce Barker" <brubar_nospamplease_@.safeco.com> д?
news:#ZFWaGRQFHA.244@.TK2MSFTNGP12.phx.gbl...
> when you do new RegInfo() you are only creating an instance of the code
> behind of your user control, which normally doesn't have any code to
create
> the controls it contains (these are usually on the aspx page that get
> compiled). Page.LoadControl("RegInfo.ascx") creates an instance of the
> actual user control (which inhertis from the code behind).
> -- bruce (sqlwork.com)
>
> "alecyy" <cityyokel@.163.com> wrote in message
> news:OX$bJQMQFHA.1884@.TK2MSFTNGP15.phx.gbl...
> > hi,
> > I create a UserControl called "RegInfo.ascx",
> > then i add a PlaceHolder to WebForm1,
> > and in WebForm1.Page_Load() i wrote the follow statement:
> > placeHolder.Controls.Add(new RegInfo());
> > but when i pressed F5 to run the web app, it shows nothing. :(
> > after search web to find out solution, i replaced upper statement with:
> > placeHolder.Controls.Add(Page.LoadControl("RegInfo.ascx"));
> > it work as my expect!
> > I am so confused about the two results,
> > Don't they add a new instance of RegInfo into the placeHolder?
> > Why one can work exactly while anthor don't?
thx.
i got it.
"Bruce Barker" <brubar_nospamplease_@.safeco.com> д?
news:#ZFWaGRQFHA.244@.TK2MSFTNGP12.phx.gbl...
> when you do new RegInfo() you are only creating an instance of the code
> behind of your user control, which normally doesn't have any code to
create
> the controls it contains (these are usually on the aspx page that get
> compiled). Page.LoadControl("RegInfo.ascx") creates an instance of the
> actual user control (which inhertis from the code behind).
> -- bruce (sqlwork.com)
>
> "alecyy" <cityyokel@.163.com> wrote in message
> news:OX$bJQMQFHA.1884@.TK2MSFTNGP15.phx.gbl...
> > hi,
> > I create a UserControl called "RegInfo.ascx",
> > then i add a PlaceHolder to WebForm1,
> > and in WebForm1.Page_Load() i wrote the follow statement:
> > placeHolder.Controls.Add(new RegInfo());
> > but when i pressed F5 to run the web app, it shows nothing. :(
> > after search web to find out solution, i replaced upper statement with:
> > placeHolder.Controls.Add(Page.LoadControl("RegInfo.ascx"));
> > it work as my expect!
> > I am so confused about the two results,
> > Don't they add a new instance of RegInfo into the placeHolder?
> > Why one can work exactly while anthor don't?
0 comments:
Post a Comment