For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > April 2007 > Re: How can I pass parameters to a user control that is loaded at into a Placeholder









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: How can I pass parameters to a user control that is loaded at into a Placeholder
Eliyahu Goldin

2007-04-29, 4:17 am

You already have a reference to the control in variable c. You just need to
typecast it to your type. One of the options:

MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.MyProperty = myValue;



--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"keith" <kbrickey@dslextreme.com> wrote in message
news:u6qg%23CjiHHA.4844@TK2MSFTNGP02.phx.gbl...
>I use this code to load a user control at runtime:
>
> Control c = Page.LoadControl(Request.ApplicationPath +
> "/User_Controls/website_design.ascx");
> UserControlPlaceHolder.Controls.Add(c);
>
> Immediately after loading the control, I need to set a property of the
> user control.
> I have tried various ways of directly referencing the control without
> success. I have also tried using FindControl, but all I get is a null
> value. Any suggestions?
>
> Thanks,
>
> Keith
>



keith

2007-04-29, 4:17 am

Thanks for your help. I changed my code to:

MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.Item = "abc";
UserControlPlaceHolder.Controls.Add(c);

When I build the page, I get the following error messages:

The type or namespace name 'MyControl' could not be found (are you missing a
using directive or an assembly reference?)

The best overloaded method match for
'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)' has some
invalid arguments

Argument '1': cannot convert from 'MyControl' to 'System.Web.UI.Control'

What am I doing wrong here?

Thanks,

Keith



"Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldDinN@mMvVpPsS.org> wrote in
message news:ecbm2MjiHHA.4676@TK2MSFTNGP02.phx.gbl...
> You already have a reference to the control in variable c. You just need
> to typecast it to your type. One of the options:
>
> MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
> "/User_Controls/website_design.ascx");
> c.MyProperty = myValue;
>
>
>
> --
> Eliyahu Goldin,
> Software Developer & Consultant
> Microsoft MVP [ASP.NET]
> http://msmvps.com/blogs/egoldin
> http://usableasp.net
>
>
> "keith" <kbrickey@dslextreme.com> wrote in message
> news:u6qg%23CjiHHA.4844@TK2MSFTNGP02.phx.gbl...
>
>



Eliyahu Goldin

2007-04-29, 4:17 am

Did you include the control in the page?

How to: Include a User Control in an ASP.NET Web Page
http://msdn2.microsoft.com/en-us/library/sbz9etab.aspx


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"keith" <kbrickey@dslextreme.com> wrote in message
news:uMCcyZjiHHA.1216@TK2MSFTNGP03.phx.gbl...
> Thanks for your help. I changed my code to:
>
> MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
> "/User_Controls/website_design.ascx");
> c.Item = "abc";
> UserControlPlaceHolder.Controls.Add(c);
>
> When I build the page, I get the following error messages:
>
> The type or namespace name 'MyControl' could not be found (are you missing
> a using directive or an assembly reference?)
>
> The best overloaded method match for
> 'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)' has some
> invalid arguments
>
> Argument '1': cannot convert from 'MyControl' to 'System.Web.UI.Control'
>
> What am I doing wrong here?
>
> Thanks,
>
> Keith
>
>
>
> "Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldDinN@mMvVpPsS.org> wrote in
> message news:ecbm2MjiHHA.4676@TK2MSFTNGP02.phx.gbl...
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com