Professional Web Applications Themes

Dynamic creation of UserControls - Problem with postback - ASP.NET Building Controls

I have created a UserControl, and I am dynamically adding it to each row of a datagrid with a sub called by the datagrid's 'OnItemDataBound'. That part works. 'creates the instance of the control Dim ctl As MyCtl = CType(LoadControl("MyCtl.ascx"), MyCtl) 'adds it to the datagrid inside a template column e.Item.Cells(1).Controls.Add(ctl ) The problem is that the user control gone postback is performed. For instance, the user control has a button, and code handling the click event of that button. The code for the click never gets executed. The control is lost on postback. That can be fixed by performing ...

  1. #1

    Default Dynamic creation of UserControls - Problem with postback

    I have created a UserControl, and I am dynamically adding
    it to each row of a datagrid with a sub called by the
    datagrid's 'OnItemDataBound'. That part works.

    'creates the instance of the control
    Dim ctl As MyCtl = CType(LoadControl("MyCtl.ascx"), MyCtl)
    'adds it to the datagrid inside a template column
    e.Item.Cells(1).Controls.Add(ctl )

    The problem is that the user control gone postback is
    performed. For instance, the user control has a button,
    and code handling the click event of that
    button. The code for the click never gets executed. The
    control is lost on postback.

    That can be fixed by performing the binding on the
    datagrid again, which triggers the dynamic creation of
    the control again, and the click even code is executed.
    The problem with that solution is that there is a
    database round trip, and the entire page is rendered
    again all to perform a simple click event.

    Any ideas? Is there different way to create the user
    controls, so they persist? How about a better way of
    doing what I need? I would just use item templates, but I
    need datagrids imbeded within rows of other datagrids. If
    you dynamically create a datagrid and add it to a cell of
    another grid, you dont have the ability to modify item
    templates of the dynamically created grid. (or am I wrong
    about that too).

    Any help appreciated,
    Roger

    Roger Guest

  2. #2

    Default Dynamic creation of UserControls - Problem with postback

    uh, sorry.. I butchered that 3rd paragraph pretty bad. I
    was cutting and pasting too much, and should have proof
    read. Here is the whole post again...

    I have created a UserControl, and I am dynamically adding
    it to each row of a datagrid with a sub called by the
    datagrid's 'OnItemDataBound'. That part works.

    'creates the instance of the control
    Dim ctl As MyCtl = CType(LoadControl("MyCtl.ascx"), MyCtl)
    'adds it to the datagrid inside a template column
    e.Item.Cells(1).Controls.Add(ctl )

    The problem is that the user control is gone once the
    postback is performed. For instance, the user control has
    a button, and code handling the click event of that
    button. The code for the click never gets executed. The
    control is lost on postback.

    That can be fixed by performing the binding on the
    datagrid again, which triggers the dynamic creation of
    the control again, and the click even code is executed.
    The problem with that solution is that there is a
    database round trip, and the entire page is rendered
    again all to perform a simple click event.

    Any ideas? Is there different way to create the user
    controls, so they persist? How about a better way of
    doing what I need? I would just use item templates, but I
    need datagrids imbeded within rows of other datagrids. If
    you dynamically create a datagrid and add it to a cell of
    another grid, you dont have the ability to modify item
    templates of the dynamically created grid. (or am I wrong
    about that too).

    Any help appreciated,
    Roger

    ..



    >-----Original Message-----
    >I have created a UserControl, and I am dynamically
    adding
    >it to each row of a datagrid with a sub called by the
    >datagrid's 'OnItemDataBound'. That part works.
    >
    >'creates the instance of the control
    >Dim ctl As MyCtl = CType(LoadControl("MyCtl.ascx"),
    MyCtl)
    >'adds it to the datagrid inside a template column
    >e.Item.Cells(1).Controls.Add(ctl )
    >
    >The problem is that the user control gone postback is
    >performed. For instance, the user control has a button,
    >and code handling the click event of that
    >button. The code for the click never gets executed. The
    >control is lost on postback.
    >
    >That can be fixed by performing the binding on the
    >datagrid again, which triggers the dynamic creation of
    >the control again, and the click even code is executed.
    >The problem with that solution is that there is a
    >database round trip, and the entire page is rendered
    >again all to perform a simple click event.
    >
    >Any ideas? Is there different way to create the user
    >controls, so they persist? How about a better way of
    >doing what I need? I would just use item templates, but
    I
    >need datagrids imbeded within rows of other datagrids.
    If
    >you dynamically create a datagrid and add it to a cell
    of
    >another grid, you dont have the ability to modify item
    >templates of the dynamically created grid. (or am I
    wrong
    >about that too).
    >
    >Any help appreciated,
    >Roger
    >
    >.
    >
    Roger Guest

  3. #3

    Default Re: Dynamic creation of UserControls - Problem with postback

    Thanks, you were right. The other problem I had is that
    the user control was being fed by data from the
    datasource of the grid. I am using the OnItemCreated to
    call a sub that adds the user control to the datagrid.
    Next, I am using OnItemDataBound to call a sub that finds
    the user control and bind's it's controls to data from
    the datagrid. Works great. Now I am saving the round trip
    to the server.

    I am actually loading another datagrid into the first
    user control, and loading another control into that
    datagrid. Datagrid > loads user control with 2nd datagrid
    > 2nd datagrid loads another user control. Works great.
    Thanks for your help.


    >-----Original Message-----
    >Hi Roger
    >
    >I think you should create your usercontrols in response
    to the datagrid's
    >ItemCreated event. This fires every time the datagrid is
    rendered, not just
    >when its databound. Your controls should then get
    created every time the
    >page is hit, and early enough that they will track their
    viewstate and raise
    >events properly, without going near the database.
    >
    >HTH
    >Joe
    >
    >"Roger" <rogerguesshotmail.com> wrote in message
    >news:036101c34905$4578bab0$a301280aphx.gbl...
    >> uh, sorry.. I butchered that 3rd paragraph pretty bad.
    I
    >> was cutting and pasting too much, and should have proof
    >> read. Here is the whole post again...
    >>
    >> I have created a UserControl, and I am dynamically
    adding
    >> it to each row of a datagrid with a sub called by the
    >> datagrid's 'OnItemDataBound'. That part works.
    >>
    >> 'creates the instance of the control
    >> Dim ctl As MyCtl = CType(LoadControl("MyCtl.ascx"),
    MyCtl)
    >> 'adds it to the datagrid inside a template column
    >> e.Item.Cells(1).Controls.Add(ctl )
    >>
    >> The problem is that the user control is gone once the
    >> postback is performed. For instance, the user control
    has
    >> a button, and code handling the click event of that
    >> button. The code for the click never gets executed. The
    >> control is lost on postback.
    >>
    >> That can be fixed by performing the binding on the
    >> datagrid again, which triggers the dynamic creation of
    >> the control again, and the click even code is executed.
    >> The problem with that solution is that there is a
    >> database round trip, and the entire page is rendered
    >> again all to perform a simple click event.
    >>
    >> Any ideas? Is there different way to create the user
    >> controls, so they persist? How about a better way of
    >> doing what I need? I would just use item templates,
    but I
    >> need datagrids imbeded within rows of other datagrids.
    If
    >> you dynamically create a datagrid and add it to a cell
    of
    >> another grid, you dont have the ability to modify item
    >> templates of the dynamically created grid. (or am I
    wrong
    >> about that too).
    >>
    >> Any help appreciated,
    >> Roger
    >>
    >> .
    >>
    >>
    >>
    >>
    >> >-----Original Message-----
    >> >I have created a UserControl, and I am dynamically
    >> adding
    >> >it to each row of a datagrid with a sub called by the
    >> >datagrid's 'OnItemDataBound'. That part works.
    >> >
    >> >'creates the instance of the control
    >> >Dim ctl As MyCtl = CType(LoadControl("MyCtl.ascx"),
    >> MyCtl)
    >> >'adds it to the datagrid inside a template column
    >> >e.Item.Cells(1).Controls.Add(ctl )
    >> >
    >> >The problem is that the user control gone postback is
    >> >performed. For instance, the user control has a
    button,
    >> >and code handling the click event of that
    >> >button. The code for the click never gets executed.
    The
    >> >control is lost on postback.
    >> >
    >> >That can be fixed by performing the binding on the
    >> >datagrid again, which triggers the dynamic creation of
    >> >the control again, and the click even code is
    executed.
    >> >The problem with that solution is that there is a
    >> >database round trip, and the entire page is rendered
    >> >again all to perform a simple click event.
    >> >
    >> >Any ideas? Is there different way to create the user
    >> >controls, so they persist? How about a better way of
    >> >doing what I need? I would just use item templates,
    but
    >> I
    >> >need datagrids imbeded within rows of other datagrids.
    >> If
    >> >you dynamically create a datagrid and add it to a cell
    >> of
    >> >another grid, you dont have the ability to modify item
    >> >templates of the dynamically created grid. (or am I
    >> wrong
    >> >about that too).
    >> >
    >> >Any help appreciated,
    >> >Roger
    >> >
    >> >.
    >> >
    >
    >
    >.
    >
    Roger Guest

Similar Threads

  1. dynamic xml creation
    By mixey18 in forum Macromedia Flash Data Integration
    Replies: 3
    Last Post: December 13th, 07:01 PM
  2. dynamic tag creation
    By JJBBDD in forum Coldfusion - Advanced Techniques
    Replies: 2
    Last Post: July 14th, 08:43 PM
  3. After PostBack my UserControls Property doesn't work correct
    By Andreas Klemt in forum ASP.NET General
    Replies: 0
    Last Post: July 8th, 02:58 PM
  4. Dynamic columns creation
    By Antonio Maciel in forum Microsoft SQL / MS SQL Server
    Replies: 1
    Last Post: July 6th, 10:47 PM
  5. Disconnected recordset dynamic creation problem
    By Cowboy \(Gregory A. Beamer\) in forum ASP
    Replies: 0
    Last Post: July 3rd, 05:26 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