How do I dynamically create user controls?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David J. Berman

    How do I dynamically create user controls?

    Thanks for any help...!

    My error is:
    Object reference not set to an instance of an object.[color=blue]
    > public int DisplayOrder {
    > get {[color=green][color=darkred]
    >>>>>> return (int) ViewState["DisplayOrd er"];[/color][/color]
    > }[/color]


    Here's the setup:
    I have made a user control that displays a persons picture. Its like this:

    AccountName
    [PICTURE]
    < 1 of 3 >

    You can click the arrows to navigate pictures. The control is smart - you
    assign the AccountName property and it hits the database, maintains state
    with ViewState. I can put several of them on one page if I put them there
    statically. When I do this dynamically, I get the above error.

    Question:
    I have made a repeater which takes a dataset that has a list of account
    names in it. It creates one instance of my control for each name in the
    list. My controls appear and render correctly. However, I get the above
    error when you click on my navigation buttons. Part of the problem is each
    instance of my user control has the same ID.

    #1 - How can I get the repeater to assign a unique id for each control?

    #2 - Is it the case that the state information won't be managed for me
    because my controls are dynamic?

    When I put multiple instances of my control statically on a page, it works
    fine. When its in the repeater, it renders fine but then clicking on the
    nav buttons to navigate pictures produces the error.

    Thank you so much!


  • David J. Berman

    #2
    Re: How do I dynamically create user controls?

    Thanks for the tip! I read about this on MSDN, google'd some examples and I
    was able to get it to work. However, there is a glitch:

    Suppose I have 10 instances of my picture control with the navigation
    buttons underneath (because each person can have multiple pictures). When I
    click those buttons each control updates correctly, however, the page seems
    to refresh and scroll to the top. Can I make it so that the page doesn't
    refresh but the control repaints itself?

    My architecture is to have a .aspx page with a PersonList user control
    sitting on it. That control is passed a list of accountnames (arraylist)
    and for each name it loads a PersonPicture user control dynamically and adds
    it to the page.

    Thanks!



    "RMD" <rmd@nospam.sor ry.com> wrote in message
    news:ekV8kZzODH A.1072@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Check out the Page.LoadContro l method.
    >
    > RMD
    >
    > "David J. Berman" <David_Berman@b ose.com> wrote in message
    > news:ZjjKa.1$3j 4.90@paloalto-snr1.gtei.net.. .[color=green]
    > > Thanks for any help...!
    > >
    > > My error is:
    > > Object reference not set to an instance of an object.[color=darkred]
    > > > public int DisplayOrder {
    > > > get {
    > > >>>>>> return (int) ViewState["DisplayOrd er"];
    > > > }[/color]
    > >
    > >
    > > Here's the setup:
    > > I have made a user control that displays a persons picture. Its like[/color][/color]
    this:[color=blue][color=green]
    > >
    > > AccountName
    > > [PICTURE]
    > > < 1 of 3 >
    > >
    > > You can click the arrows to navigate pictures. The control is smart -[/color][/color]
    you[color=blue][color=green]
    > > assign the AccountName property and it hits the database, maintains[/color][/color]
    state[color=blue][color=green]
    > > with ViewState. I can put several of them on one page if I put them[/color][/color]
    there[color=blue][color=green]
    > > statically. When I do this dynamically, I get the above error.
    > >
    > > Question:
    > > I have made a repeater which takes a dataset that has a list of account
    > > names in it. It creates one instance of my control for each name in the
    > > list. My controls appear and render correctly. However, I get the[/color][/color]
    above[color=blue][color=green]
    > > error when you click on my navigation buttons. Part of the problem is[/color]
    > each[color=green]
    > > instance of my user control has the same ID.
    > >
    > > #1 - How can I get the repeater to assign a unique id for each control?
    > >
    > > #2 - Is it the case that the state information won't be managed for me
    > > because my controls are dynamic?
    > >
    > > When I put multiple instances of my control statically on a page, it[/color]
    > works[color=green]
    > > fine. When its in the repeater, it renders fine but then clicking on[/color][/color]
    the[color=blue][color=green]
    > > nav buttons to navigate pictures produces the error.
    > >
    > > Thank you so much!
    > >
    > >[/color]
    >
    >[/color]


    Comment

    Working...