Last post Oct 26, 2009 02:37 AM by Allen Chen – MSFT
Oct 22, 2009 12:25 PM|JohnWordsworth|LINK
Sorry if this is a basic question, but I've spent the last couple of hours scouring the internet for a solution or discussion about my problem and I've come up dry. Granted, I might be searching for the wrong thing, but I hope someone here can help.
I am building an ASP.Net Web Application (C#) with Visual Web Developer 2008. I am hoping to build a site that is comprised of a large number of server controls (probably around a hundred). Now, I'm just mocking up some trial controls etc at the moment,
and I was wondering if someone could advise on what might be the best way to access data from Server Controls that are compiled into a separate library?
The ways that I have thought of so far are...
1. When each new module (collection of server controls) is added, you configure it with the Data Base ConnectionString somehow and do all of the data management completely separate to the 'parent' application.
2. Setup the Data Source in the main project and perhaps pass the ID to my code? The thing about this, is that the DataSource defines how to 'SELECT' / 'UPDATE' etc. Which means that the functions to saving and loading data in the control is actually defined
in the host project (seems wrong - as the control-set will have a definite structure for the data it requires).
I guess I should be looking to load up a DataSet in the ServerControl but using a connection defined in the main application. Without brutally passing the DatabaseConnection String through each control (seems messy), I don't know how best to do this...
Any advice would be greatly appreciated!
"Server Control Library"
Oct 26, 2009 02:37 AM|Allen Chen – MSFT|LINK
You can expose two properties. The first is called DataSource (of object type), the second is called DataSourceID (of string type). Then control users can decide which property to use to populate this custom data presentation control.
For DataSource (of object type) you can check if it's IEnumerable. If yes, use foreach to loop through it to populate control. If not, use reflection to get all properties of this object to populate control. You can also do other check if you like.
For DataSourceID (of type string), use FindControl method to find the DataSourceControl, use it's Select method to populate your control.