Trishit Bera
Trishit Bera

Reputation: 101

How to find controls in frame?

In my app I have one master page with frame, page1 and page2. When the app starts, the master page loads with page1 in a frame. At that time I am able to find the controls (TextBox, GridView) in page1 using the following code in masterpage.

private  void Page_Loaded(object sender, RoutedEventArgs e)
{
    Frame rootFrame = Window.Current.Content as Frame;
    Page mainPage = rootFrame.Content as MasterPage;
    Frame myframe = mainPage.FindName("frameBody") as Frame;          
    Page page1 = frame.Content as Page1;
    Page page2 = frame.Content as Page2;
    Button btnpage = page1.FindName("btnPage2") as Button;
}

When I navigate page2 or Load the page2 in the frame At that time i am not able to find the controls (TextBox, GridView) in page2 using the same code.

Upvotes: 0

Views: 660

Answers (1)

DotNetRussell
DotNetRussell

Reputation: 9857

When you do Frame.Navigate(newPage) you are releasing all references to the old content and it gets garbage collected. If you want to persist data you need to cache the data. If you need to keep the controls, then you probably need to rethink your architecture.

The answer to your question is though, the reason you can't see the controls is because they no longer exist in the visual tree. They are removed and garbage collected. The way around this is to cache them.

Reference Information:

MSDN Frame Documentation

Reference Source - Frame.cs

and since your next question will be how to persist data between pages, check out

MSDN Frame.Navigate(Type,Object) Documentation

Upvotes: 1

Related Questions