Getting objects on UserControl from containing page OR refreshing User Control on property change RRS feed

  • Question

  • User-2109111975 posted

    I'm sure this question has been asked before, but my google skills are failing me...

    I have a page with 4 panels on it... Once a user has selected some values in Panel 1, they can navigate back and forth between the Panels.

    One of the Panels will contain a UserControl; the UserControl has a GridView and a public property (public int thisId{ get; set; })

    When a user navigates to this Panel, I would like the User Control to update itself based on the public property defined above.

    Currently, I am adding the User Control dynamically to the Panel whenever a user navigates to it and sending the property value at the same time like so inside an OnCommand Event from a Button:

                Control ctrl = LoadControl("~/Controls/myUserControl.ascx");
                ((myControl)ctrl).thisId= Int32.Parse(hfThisId.Value);

    phStep4 is a Place Holder inside of the Panel, and hfThisId is a Hidden Field with the Id generated from the first Panel.

    This correctly puts the control on the page, and it behaves properly according to the Id that I pass.

    My issue is that, when a user navigates away from this page I need to find the GridView object and get some values from it... but I can't seem to find the GridView...
    I have tried GridView gv = (GridView)phStep4.FindControl("gv1"), but this results in gv = null
    The Controls collection in phStep4 has a Count = 0

    I'm figuring that, b/c I'm adding the control on a button_command event it is not in the ViewState when I click another button causing a postback.

    I have also tried adding the UserControl in markup...
    <uc1:myUserControl ID="EAS" runat="server" />
    This will let me grab the GridView using gv = EAS.FindControl("gv1"), but I can't seem to make the UserControl behave as I want when I set the public property value:
    (also within a Button_OnCommand event)

                EAS.thisId = Int32.Parse(hfThisId.Value);

    What is the proper way to go about this?
    If I add the UserControl programatically, does it have to be in OnInit?
    If I add it in markup, how do I make it refresh itself?


    Tuesday, September 13, 2011 5:13 PM


All replies