Dynamic creation of UserControls - Problem with postback

R

Roger

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
 
R

Roger

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

..
 
R

Roger

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top