Dynamic creation of UserControls - Problem with postback

Discussion in 'ASP .Net Building Controls' started by Roger, Jul 13, 2003.

  1. Roger

    Roger Guest

    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, Jul 13, 2003
    #1
    1. Advertising

  2. Roger

    Roger Guest

    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, Jul 13, 2003
    #2
    1. Advertising

  3. Roger

    Roger Guest

    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" <> wrote in message
    >news:036101c34905$4578bab0$...
    >> 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, Jul 13, 2003
    #3
    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. Andreas Klemt
    Replies:
    0
    Views:
    401
    Andreas Klemt
    Jul 8, 2003
  2. Gummy
    Replies:
    0
    Views:
    337
    Gummy
    Sep 19, 2006
  3. CreativeMind
    Replies:
    2
    Views:
    878
    Andrew Morton
    Dec 10, 2009
  4. Kelly Leahy

    v2.0: UserControls, Postback, and WebParts

    Kelly Leahy, May 2, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    163
    Kelly Leahy
    May 2, 2006
  5. Peter Kirk

    usercontrols and postback

    Peter Kirk, Aug 14, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    116
    clintonG
    Aug 14, 2006
Loading...

Share This Page