I have a silverlight page which has about 5 user controls. Most of the controls are getting the same data from the database which I wanted to save some round trips to DB. I wanted to get the data in the main page and then pass it to the child controls. I tried creating a public property in the controls and setting them from the main page.
What is the best way of doing this?
Thanks.
From your description, it sounds like data binding would be your best approach (MSDN documentation). If you set the main control's DataContext to the object you retrieved from the database, all of the controls in that page will have access to the DataContext as well. This would also allow you to leverage data binding expressions to populate the properties of the UserControls' children.
Related
I am using a few DataGridViews with BindingSources. One of these DataGridViews is used to display details for a Child Property of another BindingSource.
The child object details are not immediately displayed on the screen (I'm using DevExpress XtraTabControl) and I want to load the child property only when the user displays the tab of that child property.
The child property is retrieved from the database, but as this can take a while it is only loaded the when it is first accessed and subsequent retrievals access the now cached object.
The problem seems to be that creating the BindingSource bindings immediately accesses the Child Property (and therefore accesses the database for every child property, and there are quite a few).
Is it possible to have Child Binding Sources only access the property when it is being displayed?
You can connect your BindingSource to your database at the convenient time, for example when handling the XtraTabControl.SelectedPageChanged or TabControl.SelectionChanged event.
Just set its DataSource property:
myBindingSource.DataSource=myDataSource;
I am building a control heavy application in wpf and I am trying use the best solution for various views (in terms of maintainability, scalability, etc).
Eg If I have a control on a main view and I want to bind data between them I use a dependency property.
My question is what is the best tool to use if I have a control (lets call it PaneControl) on a view (call it MainView), which itself contains a control (call it BasicControl) with a property on it which I want to pass to the main view.
Is there a way of passing the property from the BasicControl to the MainView without sending a command to PaneControl and then sending another command to MainView?
Best way to transfer data from view to complex controls would be still Binding ElementName.
Here is a link to a page that explains how to use Binding ElementName.
http://msdn.microsoft.com/en-us/library/system.windows.data.binding.elementname%28v=vs.110%29.aspx
If ElementName is not what you looking for then maybe x:Reference will help you out.
Take a look at this link:
http://msdn.microsoft.com/en-us/library/ee795380%28v=vs.110%29.aspx
I created a CustomDataBoundControl. I implemented CreateChildControls(), but I am not actively doing anything to support viewstate and thus, I loose the data after a postback. I was looking in to this article: http://msdn.microsoft.com/en-us/library/aa479016.aspx
My problem is that the way the sample works with ViewState is using a Pair object, which holds key-value pairs and recreates a known data type in the collection of itemsIStateManager` implementation.
My control is more generic. I do not know the type of the objects coming in to the datasource property and I create the child controls via reflection in to the datasource objects. I create a Table control and fill it with TableRow controls, which I fill with TableCell controls.
I can't seem to bridge this gap between Dino`s example and my real-world implementation.
When it comes to data source controls, you don't use ViewState to retain the data. As long as your building a control tree, the .NET framework will reload the viewstate of the controls in the control state. You just need to store the viewstate for the core properties of the control, plus the number of items rendered. This way, you can rerender the same control tree.
create the control tree on init to maintain the ViewState, you need not populate the data in your controls but just create the controls. As per the page flow ViewState is enabled for the controls which are created in OnInit. Hope this helps.
I have a UserControl that consists of a Parent and Child UserControls that is displayed on a aspx page. I need to get the instance of the Parent UserControls from the child controls. The Parent has a set of nested .net controls and in these nested controls the child UserControls are displayed so if I use this from a child UserControls
MyControl _myControl = (MyControl)this.Parent.Parent.Parent.Parent.FindControl("MyControl");
where (this) = the child control and (Parent.Parent.Parent.Parent) walks me back the tree to the real parent.
This will get me there but there just seems to be a better way. Any suggestions?
An ascx shouldn't know anything about its parent(s): that's a sign that it's too closely coupled to other classes. They might as well be one class.
One alternative is to follow the law of Demeter: figure out what this (your user control) needs from MyControl, make it a property, and let your aspx provide it rather than asking for it.
If you're using a master page, you can start from there and use the container id to find the control. It just depends what the control is closer to.
This might help:
http://www.asp.net/master-pages/tutorials/control-id-naming-in-content-pages-cs
Another thing you can do if you're accessing a value from a control in a parent page, is to put that value into the HttpContext.Current.Items["MyControlValue"] on page load. That way your usercontrol can grab that value easily
I want to have my user controls have design time data. If I add the data/datacontext using the constructor, I can see the data in the control from a different view that contains the control.
If I set the DataContext from xaml, I can see it when I'm designing the control, but I see nothing in a control that hosts the control.
So is there any example of how to get design time data to show up in a control if it is being edited, or it's parent is being edited? Or anything that lists the rules of then the constructor is run/not run from the designer? I'm trying to set a DesignViewModel with the data, and at runtime use the actual view model.
If I understand your question correctly then it's answered in this question