This is a migrated thread and some comments may be shown as answers.

How to access control in template before control.Loaded

1 Answer 75 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Lasse
Top achievements
Rank 1
Lasse asked on 11 Nov 2011, 02:06 AM
Hi there,

I have a RadDataForm with a template, which contains a TabControl and some controls on each TabPage. On two of the TabPages I have a GridView that I would like to populate as soon as the RadDataForm has loaded.

The Loaded event on the GridViews are not fired until the TabPage is clicked, which means that I cannot get a reference to the GridView control, before the TabPage is clicked.

How do I get a reference to the GridViews in order to populate them, before clicking the TabPage?

Thanks in advance!

1 Answer, 1 is accepted

Sort by
0
Lasse
Top achievements
Rank 1
answered on 11 Nov 2011, 03:17 AM
I found a better way of accessing the controls in the TabPages - here is how I did it.

Set the Loaded event on the TabControl (<telerik:RadTabControl Loaded="RadTabControl_Loaded" >) and in that event handler, assign the TabControl to a RadTabControl attribute in code behind like so:

RadTabControl _tabControl;
  
void RadTabControl_Loaded(object sender, RoutedEventArgs e)
{
    _tabControl = sender as RadTabControl;
}

Then you have access to the TabItems AND the controls in their respective contents. From here it is simple enough to get the controls you need - I wrote a small generic extension method for getting the control I need based on the type and name (if any) of the control:

public static object GetTabPageItem(this RadTabControl tabControl, Type t, string name = "")
{
    foreach(RadTabItem tabItem in tabControl.Items.Where(ti => ((RadTabItem)ti).Content.GetType() == t))
    {
        if(name != "")
        {
            var p = tabItem.Content.GetType().GetProperty("Name");
            if(p == null)
            {
                continue;
            }
            string pName = (string)p.GetValue(tabItem.Content, null);
            if(pName != name)
            {
                continue;
            }
        }
        return tabItem.Content;
    }
    return null;
}


Then I added easy access to the controls, by implementing properties for each control:

UserAccounts UserAccountsView
{
    get
    {
        if(_tabControl == null)
            return null;
        return (UserAccounts)_tabControl.GetTabPageItem(typeof(UserAccounts));
    }
}
UserRoles UserRolesView
{
    get
    {
        if(_tabControl == null)
            return null;
        return (UserRoles)_tabControl.GetTabPageItem(typeof(UserRoles));
    }
}

And finally I was able to do the following on CurrentItemChanged:

void RadDataForm_CurrentItemChanged(object sender, EventArgs e)
{
    var user = RadDataForm.CurrentItem as Web.Models.User;
    UserAccountsView.LoadData(user.Id);
    UserRolesView.LoadData(user.Id);
}

Just thought I'd share that if someone else runs into the same problem.

This approach will probably work for all types of container controls - not only TabControls. The priciple is simple - when the Loaded event of the container fires, assign the container control to a variable that you can use to get access to controls in the container.

Tags
DataForm
Asked by
Lasse
Top achievements
Rank 1
Answers by
Lasse
Top achievements
Rank 1
Share this question
or