Search code examples
asp.netcontrolsviewstate

Pass data to user components in asp.net


It is .net 2.0 here, not MVC, and I am crap at asp forms.

I have a page with user controls on it. When I click on something in the page, I want to load the usercontrol based on a parameter from the page.

I cannot do it.

In my page's FaultTree_Clicked, I get the value, then:

  • I tried exposing a property on the child user control to set the value, which i set in FaultTree_Clicked, it gets forgotten.

  • I tried saving it to Session["mykey"], and loading Session["mykey"] in the control's Page_init... the value is blank.

  • I tried saving it to ViewState["mykey"], and loading ViewState["mykey"] in the control's Page_init... the value is blank.

EDIT: more specific info:

Here is a cut down version of what the page(MyFault) looks like:

<form id="form" runat="server">
 <div id="faulttree">
   <asp:TreeView ID="FaultTree" ......>
 </div>
 <uc1:_DefectDetail ID="DefectDetail" runat="server" Visible="true" EnableViewState="true" />
</form>

And there is a method on the pages code behind "FaultTree_SelectedNodeChanged()".

When that method is hit, I want to load/show the DefectDetail control. The DefectControl requires a faultid, which comes off the Tree, which I successfully get in the SelectedNodeChanged method. I cannot get the faultid into the defect control.


Solution

  • This has to do with ASP.NET page lifecycle. By the time the click event fires, the control's init event has already happened.

    In order to better assist you, please provide a more detailed explanation of what the FaultTree control is, what is the desired result and some sample code.

    UPDATE:

    Instead of a public property, you can simply create a public method in the control that does the desired action and invoke it from the FaultTree_SelectedNodeChangeEvent.

    Example (for a public method named Refresh):

    _DefectDetail.Refresh(object data);