Efficiency of dynamically adding web user controls

Discussion in 'ASP .Net' started by MC D, Nov 17, 2003.

  1. MC D

    MC D Guest

    When I dynamically add a control by doing something like

    dim x as control = Page.LoadControl("myControl.ascx")

    am I actually loading the control from disk??? I don't really understand why
    I can't access the class for the user control directly... in other words
    just say:

    dim x as new myControl()

    (which doesn't generate an error, but it wil not render the UI)

    If I'm adding a control dozens of times on a page, is this very
    in-efficient?

    Thanks for any help!

    -D
    MC D, Nov 17, 2003
    #1
    1. Advertising

  2. You don't have to use LoadControl.
    Your dim statement is working fine. You've simply forgotten to add the
    control to the page.
    For this you should use Page.Controls.Add
    Here's more info:
    http://msdn.microsoft.com/library/d...InstantiatingUserControlsProgrammatically.asp

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net
    Hire top-notch developers at http://www.able-consulting.com



    "MC D" <> wrote in message
    news:%...
    > When I dynamically add a control by doing something like
    >
    > dim x as control = Page.LoadControl("myControl.ascx")
    >
    > am I actually loading the control from disk??? I don't really understand

    why
    > I can't access the class for the user control directly... in other words
    > just say:
    >
    > dim x as new myControl()
    >
    > (which doesn't generate an error, but it wil not render the UI)
    >
    > If I'm adding a control dozens of times on a page, is this very
    > in-efficient?
    >
    > Thanks for any help!
    >
    > -D
    >
    >
    Steve C. Orr [MVP, MCSD], Nov 17, 2003
    #2
    1. Advertising

  3. MC D

    Marina Guest

    User controls bear some resemblence to include files. Of course they are
    object oriented, much more flexible, etc.

    There are many large systems that rely on include files, and those work ok -
    so I would imagine user controls wouldn't be any worse.

    "MC D" <> wrote in message
    news:%...
    > When I dynamically add a control by doing something like
    >
    > dim x as control = Page.LoadControl("myControl.ascx")
    >
    > am I actually loading the control from disk??? I don't really understand

    why
    > I can't access the class for the user control directly... in other words
    > just say:
    >
    > dim x as new myControl()
    >
    > (which doesn't generate an error, but it wil not render the UI)
    >
    > If I'm adding a control dozens of times on a page, is this very
    > in-efficient?
    >
    > Thanks for any help!
    >
    > -D
    >
    >
    Marina, Nov 17, 2003
    #3
  4. MC D

    -=Chris=- Guest

    Do not forget that some user controls are not just classes, but also contain
    html snippets in the actual ascx file. It is for the purpose of retreiving
    both the class, and the html within the ascx file, that page.loadcontrol()
    is used.

    User Controls are most helpful when creating commonly used UI components.
    If your user control has no associated html in the .ascx file, I'd recommend
    creating a simple class or a server control instead.



    I was minding my own business when MC D blurted out:

    > When I dynamically add a control by doing something like
    >
    > dim x as control = Page.LoadControl("myControl.ascx")
    >
    > am I actually loading the control from disk??? I don't really understand

    why
    > I can't access the class for the user control directly... in other words
    > just say:
    >
    > dim x as new myControl()
    >
    > (which doesn't generate an error, but it wil not render the UI)
    >
    > If I'm adding a control dozens of times on a page, is this very
    > in-efficient?
    >
    > Thanks for any help!
    >
    > -D


    --
    Insert corny line here
    -=Chris=-, Nov 18, 2003
    #4
  5. MC D

    Big D Guest

    Instantiating the class directly does not render the interface.

    -D
    "Steve C. Orr [MVP, MCSD]" <> wrote in message
    news:...
    > You don't have to use LoadControl.
    > Your dim statement is working fine. You've simply forgotten to add the
    > control to the page.
    > For this you should use Page.Controls.Add
    > Here's more info:
    >

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
    l/cpconInstantiatingUserControlsProgrammatically.asp
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > http://Steve.Orr.net
    > Hire top-notch developers at http://www.able-consulting.com
    >
    >
    >
    > "MC D" <> wrote in message
    > news:%...
    > > When I dynamically add a control by doing something like
    > >
    > > dim x as control = Page.LoadControl("myControl.ascx")
    > >
    > > am I actually loading the control from disk??? I don't really understand

    > why
    > > I can't access the class for the user control directly... in other words
    > > just say:
    > >
    > > dim x as new myControl()
    > >
    > > (which doesn't generate an error, but it wil not render the UI)
    > >
    > > If I'm adding a control dozens of times on a page, is this very
    > > in-efficient?
    > >
    > > Thanks for any help!
    > >
    > > -D
    > >
    > >

    >
    >
    Big D, Nov 18, 2003
    #5
    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. Jason
    Replies:
    7
    Views:
    1,208
    Rajesh CKR
    May 4, 2006
  2. tshad
    Replies:
    2
    Views:
    451
    tshad
    Jul 27, 2006
  3. Bob Rock
    Replies:
    1
    Views:
    625
    Cowboy \(Gregory A. Beamer\)
    Sep 17, 2006
  4. Guest
    Replies:
    6
    Views:
    374
    Walter Wang [MSFT]
    Dec 23, 2006
  5. Justin Dutoit
    Replies:
    0
    Views:
    347
    Justin Dutoit
    Mar 19, 2008
Loading...

Share This Page