473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How can I pass parameters to a user control that is loaded at into a Placeholder at runtime?

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
Apr 29 '07 #1
9 3673
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.ApplicationPat h +
"/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" <kb******@dslextreme.comwrote in message
news:u6****************@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

Apr 29 '07 #2
keith wrote:
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
Hi Keith

I hope I remember this right (can't check it atm).
Try to set the id property for the control or it will have a generic id.
Apr 29 '07 #3
Thanks for your help. I changed my code to:

MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPat h +
"/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" <RE**************************@mMvVpPsS.orgwrote in
message news:ec**************@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.ApplicationPat h +
"/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" <kb******@dslextreme.comwrote in message
news:u6****************@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


Apr 29 '07 #4
Thanks for answering. How can I set the ID property for the user control?
When I look in Properties, all I see are File Name and Full Path.

-Keith
"Vili" <en*****@spammia.fiwrote in message
news:46***********************@news.fv.fi...
keith wrote:
>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

Hi Keith

I hope I remember this right (can't check it atm).
Try to set the id property for the control or it will have a generic id.

Apr 29 '07 #5
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" <kb******@dslextreme.comwrote in message
news:uM**************@TK2MSFTNGP03.phx.gbl...
Thanks for your help. I changed my code to:

MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPat h +
"/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" <RE**************************@mMvVpPsS.orgwrote in
message news:ec**************@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.ApplicationPat h +
"/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" <kb******@dslextreme.comwrote in message
news:u6****************@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



Apr 29 '07 #6
Hi

In your code behind when you set the control

Control c = Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.Id = "myId"
UserControlPlaceHolder.Controls.Add(c);

After this you should be able to find the control with FindControl("myId")

keith wrote:
Thanks for answering. How can I set the ID property for the user control?
When I look in Properties, all I see are File Name and Full Path.

-Keith
"Vili" <en*****@spammia.fiwrote in message
news:46***********************@news.fv.fi...
>keith wrote:
>>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
Hi Keith

I hope I remember this right (can't check it atm).
Try to set the id property for the control or it will have a generic id.

Apr 29 '07 #7
Thanks for helping. Taking your suggestion, using the following code,
FindControl still turns up a null;

Control c;
c = Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.ID = "website_design_user_control";
UserControlPlaceHolder.Controls.Add(c);
Control thisControl = FindControl("website_design_user_control");

"Vili" <en*****@spammia.fiwrote in message
news:46***********************@news.fv.fi...
Hi

In your code behind when you set the control

Control c = Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.Id = "myId"
UserControlPlaceHolder.Controls.Add(c);

After this you should be able to find the control with FindControl("myId")

keith wrote:
>Thanks for answering. How can I set the ID property for the user control?
When I look in Properties, all I see are File Name and Full Path.

-Keith
"Vili" <en*****@spammia.fiwrote in message
news:46***********************@news.fv.fi...
>>keith wrote:
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
Hi Keith

I hope I remember this right (can't check it atm).
Try to set the id property for the control or it will have a generic id.
Apr 29 '07 #8
Try to test if you can find the control right after you add it.

keith wrote:
Thanks for helping. Taking your suggestion, using the following code,
FindControl still turns up a null;

Control c;
c = Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.ID = "website_design_user_control";
UserControlPlaceHolder.Controls.Add(c);
Control thisControl = FindControl("website_design_user_control");
Apr 29 '07 #9
Control tp = (Control)Page.LoadControl("~/[YourControl].ascx");
tp.ID = "[YourControl]";

Type typ = tp.GetType();

System.Reflection.PropertyInfo pi = typ.GetProperty("CategoryId");

pi.SetValue(tp, [propertyName], null);

[Your PlaceHolder].Controls.Add(tp);
Aug 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: R. Ian Lee | last post by:
I figured this out several months ago and now that I need it I've misplaced my code... How do I use a placeholder control to dynamically insert a user control (.ascx) into a page? Thanks! ...
0
by: Greg Park | last post by:
I have many user controls loading into a web page using Page.LoadControl However, I'm unable to figure out how to raise an event when a button is click or a check box is checked. I can have...
1
by: Dan | last post by:
I have an asp.net page default.aspx with a user control and a placeholder control. <html> <body> <form id="myform" method="post" runat="server" /> <PageHeader:Header id="header1"...
1
by: Kris van der Mast | last post by:
Hi, been a while since I posted a question myself instead of trying to help others out. I'm refactoring an existing web app that uses dynamic loading of user controls and a lot of...
3
by: Matej Kavčič | last post by:
Hello, i have one problem. I use loadcontrol in default.aspx page. On page i have placeholder and on load i with loadcontral load dynamic some user control. Some times i must load same user...
1
by: Stu | last post by:
Hi, I have created a Web User Control (vb.net) that I want to display on a placeholder on the page when a button is pressed. The control 'DeleteImage' is a blank control with 2 buttons added at...
5
by: footballhead | last post by:
I have a site that has MANY different clothing categories. I have a page set up for displaying the products (productsearch.aspx) when a user clicks one of the category links on the navigation menu....
1
by: Joe | last post by:
Hello All, I have a user control which is composed of a label and a dropdownlist. In my code I add the user control to a placeholder on the webform. Now I want to be able to retrieve the...
3
by: Markus Kling | last post by:
Hi, I have a rich Forms UserControl which is embedded into a web page. Since installing .NET 2.0 on the clients, the control stopped loading completly. - I disabled security completly using...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.