How to pass parameters to a user control

J

Jill Graham

Hi folks,

I'm calling some custom made user controls from within my .aspx web page
using

newControl = Page.LoadControl("myControl.ascx")
Page.Controls.Add(newControl)

My control has two properties: "pageId" and "Title".
How can we pass these parameters to the control from within the main .aspx
page ?

Thanks for helping me out.

Jill
 
J

Jonathan

First of all, your ascx file must be in your application path.
So you need to give a class name to you MyControl.ascx; do it this
way:

<% Control Language= "C#" ClassName="myControl" %>

then in your aspx:

Page_Init( )
{
myControl myctrl = Page.LoadControl( "myControl.ascx" ) as
myControl;
if( myctrl != null )
{
myctrl.Title = "foo";
form1.Controls.Add( myctrl );
}
else
{
Response.Write( "Error loading control" );
}
}

Files in the application path should be compiled at runtime so the
compiler will know what "myControl" type refer to. If you don't
specify a class name, I think you should still be able to access it
this way: asp.MyControl_ascx myCtrl = Page.LoadControl(
"MyControl.ascx" );

Hope it helps!

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top