How to pass parameters to a user control

Discussion in 'ASP .Net Web Controls' started by Jill Graham, Jun 8, 2004.

  1. Jill Graham

    Jill Graham Guest

    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
    Jill Graham, Jun 8, 2004
    #1
    1. Advertising

  2. Jill Graham

    Jonathan Guest

    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




    "Jill Graham" <> wrote in message news:<O#>...
    > 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
    Jonathan, Jun 18, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henke
    Replies:
    3
    Views:
    3,556
  2. Ben
    Replies:
    5
    Views:
    28,049
    David Jessee
    May 12, 2004
  3. Replies:
    2
    Views:
    6,561
    Joshua Flanagan
    Feb 28, 2005
  4. keith
    Replies:
    9
    Views:
    2,114
    Graham Underwood
    Aug 8, 2008
  5. hmikki
    Replies:
    1
    Views:
    141
    Nathan Sokalski
    Oct 19, 2007
Loading...

Share This Page