473,790 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
UserControlPlac eHolder.Control s.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 3694
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(Re quest.Applicati onPath +
"/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******@dslex treme.comwrote in message
news:u6******** ********@TK2MSF TNGP02.phx.gbl. ..
>I use this code to load a user control at runtime:

Control c = Page.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
UserControlPlac eHolder.Control s.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.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
UserControlPlac eHolder.Control s.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(Re quest.Applicati onPath +
"/User_Controls/website_design. ascx");
c.Item = "abc";
UserControlPlac eHolder.Control s.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. ControlCollecti on.Add(System.W eb.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.orgwro te in
message news:ec******** ******@TK2MSFTN GP02.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(Re quest.Applicati onPath +
"/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******@dslex treme.comwrote in message
news:u6******** ********@TK2MSF TNGP02.phx.gbl. ..
>>I use this code to load a user control at runtime:

Control c = Page.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
UserControlPla ceHolder.Contro ls.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*****@spammi a.fiwrote in message
news:46******** *************** @news.fv.fi...
keith wrote:
>I use this code to load a user control at runtime:

Control c = Page.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
UserControlPla ceHolder.Contro ls.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******@dslex treme.comwrote in message
news:uM******** ******@TK2MSFTN GP03.phx.gbl...
Thanks for your help. I changed my code to:

MyControl c = (MyControl)Page .LoadControl(Re quest.Applicati onPath +
"/User_Controls/website_design. ascx");
c.Item = "abc";
UserControlPlac eHolder.Control s.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. ControlCollecti on.Add(System.W eb.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.orgwro te in
message news:ec******** ******@TK2MSFTN GP02.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(Re quest.Applicati onPath +
"/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******@dslex treme.comwrote in message
news:u6******* *********@TK2MS FTNGP02.phx.gbl ...
>>>I use this code to load a user control at runtime:

Control c = Page.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
UserControlPl aceHolder.Contr ols.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.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
c.Id = "myId"
UserControlPlac eHolder.Control s.Add(c);

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

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*****@spammi a.fiwrote in message
news:46******** *************** @news.fv.fi...
>keith wrote:
>>I use this code to load a user control at runtime:

Control c = Page.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
UserControlPl aceHolder.Contr ols.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.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
c.ID = "website_design _user_control";
UserControlPlac eHolder.Control s.Add(c);
Control thisControl = FindControl("we bsite_design_us er_control");

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

In your code behind when you set the control

Control c = Page.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
c.Id = "myId"
UserControlPlac eHolder.Control s.Add(c);

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

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*****@spammi a.fiwrote in message
news:46******* *************** *@news.fv.fi...
>>keith wrote:
I use this code to load a user control at runtime:

Control c = Page.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
UserControlP laceHolder.Cont rols.Add(c);

Immediatel y 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.LoadContro l(Request.Appli cationPath +
"/User_Controls/website_design. ascx");
c.ID = "website_design _user_control";
UserControlPlac eHolder.Control s.Add(c);
Control thisControl = FindControl("we bsite_design_us er_control");
Apr 29 '07 #9
Control tp = (Control)Page.L oadControl("~/[YourControl].ascx");
tp.ID = "[YourControl]";

Type typ = tp.GetType();

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

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

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

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

Similar topics

3
1202
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! Ian
0
1724
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 upto a hundred user controls on a single page and AutoPostBack isn't an option. I have no problem with raising events when an user control is dropped on the aspx page.
1
2961
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" runat="server" /> <asp:PlaceHolder ID="content" runat="server" /> </form> </body>
1
2326
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 response.redirects to the same page. Because I hate the overhead by doing this I'm searching for a cleaner option. But I'm having troubles (off course or I wouldn't be posting this).
3
1873
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 control twice, like test.ascx, ockey thats no problem. But now in test.ascx is loaded twice so how can i now determinete in load function in user control whic one is. I also want to create that when i load user contol in default.aspx write some...
1
1777
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 design time (cancel/accept). However, if I add a reponse.write("hello world") in the page load handler the text appears to shhow the control is actually loding & firing to some extent, but there is no sign of the buttons! Am I missing something...
5
2784
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. I would like to have a different user control that gets loaded as a sub-header for each category (ie, tee shirts, shoes, backpacks, etc.) The category user controls include images and links to refined searches specific to each category. So...
1
2926
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 selected value from the drop downlist when a user clicks a button server control. Here is the code that populates the user control and placeholder: Dim UControl As AugmentedDropDownList =
3
1888
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 caspol -s off => no effect - I enabled fussion logging => no single entry generated - I enabled the IEHost log => nothing created - Changed to another client machine => same issues, same results - I started writing a new UserControl from scratch...
2
15073
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 been able to find the cause of the problem, and will describe it here first textually and then through a code example. The purpose of what I am trying to do is to create a postback-free web application through the use of ASP.net AJAX UpdatePanels...
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.