Professional Web Applications Themes

How do I dynamically create user controls? - ASP.NET Building Controls

Thanks for any help...! My error is: Object reference not set to an instance of an object. > public int DisplayOrder { > get { >>>>>> return (int) ViewState["DisplayOrder"]; > } 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 ...

  1. #1

    Default How do I dynamically create user controls?

    Thanks for any help...!

    My error is:
    Object reference not set to an instance of an object.
    > public int DisplayOrder {
    > get {
    >>>>>> return (int) ViewState["DisplayOrder"];
    > }

    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 Guest

  2. #2

    Default 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" <rmdnospam.sorry.com> wrote in message
    news:ekV8kZzODHA.1072TK2MSFTNGP10.phx.gbl...
    > Check out the Page.LoadControl method.
    >
    > RMD
    >
    > "David J. Berman" <David_Bermanbose.com> wrote in message
    > news:ZjjKa.1$3j4.90paloalto-snr1.gtei.net...
    > > Thanks for any help...!
    > >
    > > My error is:
    > > Object reference not set to an instance of an object.
    > > > public int DisplayOrder {
    > > > get {
    > > >>>>>> return (int) ViewState["DisplayOrder"];
    > > > }
    > >
    > >
    > > 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 Guest

  3. #3

    Default 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" <rmdnospam.sorry.com> wrote in message
    news:ekV8kZzODHA.1072TK2MSFTNGP10.phx.gbl...
    > Check out the Page.LoadControl method.
    >
    > RMD
    >
    > "David J. Berman" <David_Bermanbose.com> wrote in message
    > news:ZjjKa.1$3j4.90paloalto-snr1.gtei.net...
    > > Thanks for any help...!
    > >
    > > My error is:
    > > Object reference not set to an instance of an object.
    > > > public int DisplayOrder {
    > > > get {
    > > >>>>>> return (int) ViewState["DisplayOrder"];
    > > > }
    > >
    > >
    > > 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 Guest

Similar Threads

  1. Create User Controls dynamically
    By David Lozzi in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: October 19th, 08:26 AM
  2. Dynamically created user controls
    By Denise in forum ASP.NET Building Controls
    Replies: 4
    Last Post: March 16th, 06:55 PM
  3. Dynamically create controls with non-hierarchical IDs
    By Dale in forum ASP.NET Building Controls
    Replies: 1
    Last Post: July 15th, 05:35 PM
  4. dynamically adding user controls
    By Dune in forum ASP.NET Building Controls
    Replies: 6
    Last Post: October 3rd, 03:53 AM
  5. Dynamically create controls from a network directory tree
    By Jereme in forum ASP.NET Building Controls
    Replies: 0
    Last Post: September 26th, 03:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139