Thursday, March 29, 2012

Passing values between User Controls

I programmatically add a user control to a page which has a data grid on it. The user can then click on a link button to drill down to detail information.

What I would like to do is unload the existing user control and add the new user control with the data grid showing the detail information.

The question is how do I pass the key values from the existing user control to the new user control ?

Thanks

MarkYou can add your Items to the CurrentContext in order to pass from one control to another or from Page to an control


Context.Items.Add("MyValue", myValue)

To retrieve


Context.Items["MyValue"]

The problem here is when I click on the LinkButton on the DataGrid its going to cause the Parent page to reload itself and in the parents Page_Load subroutine its going to unload the existing Datagrid and load up the User Control with the detail DataGrid on. Therefore, it will never pass through the LinkButton's event to set the Context value.

Unless the secret is to load up both User Controls, on the Post Back, but hide the Header DataGrid ? Or to use client side code to pass the key of the selected record to a hidden field which will get submitted back to the server and be available much sooner in the process ?

I would be interested to here what people think would be the best way of doing this or whether there is a way to still use the Context stuff.

Cheers

Mark
Hi,

you could use a delegate.This article explains it how you could do it.

Grz, Kris.
Here Context is the HttpContext which is always available in a page and user controls. You can use this on the Page Load to store some values and then retrieve it later in the User controls as shown in my previous post.

0 comments:

Post a Comment