Accessing Data Using Code


OK, so it's now time to face reality. Perhaps you want to sit down while we break the bad news to you. The pages we've used to display data in this chapter take advantage of two special controls that are not actually part of the standard .NET Framework. The SqlDataSourceControl and MxDataGrid controls are special additions to the range of standard controls, and are designed to make it easy to work with data when building pages with Web Matrix.

What this means is that the pages we've created in the previous chapter won't work unless these controls are installed into the .NET Framework class library on the server. Installing Web Matrix does this, and many hosting companies that support ASP.NET will also install them on their servers. However, it's useful (if not vital) to know how we can create the same kinds of pages without depending on these extremely useful controls. To do this, we have to write some code.

Getting Back to Basics

You'll recall that we noted in the previous chapter how the page we built that used the SqlDataSourceControl and MxDataGrid controls contained no server-side code – the Code window in the editor was empty. This is because the two controls automatically react to events that happen in the page, and extract and display the data using their own internal routines. Simply setting the DataSourceControlID property of the MxDataGrid control to the ID of the SqlDataSourceControl links them together so that no code is required to make it all work.

Behind the scenes, these two controls use the basic controls and objects that are part of the standard .NET Framework class library. The SqlDataSourceControl uses the ADO.NET DataAdapter class to access the data and create a DataSet, while the MxDataGrid control is like a clever version of the standard ASP.NET Web Forms DataGrid control.

With only a little extra effort, we can use these standard controls from the .NET Framework instead. It also provides us with more flexibility, the ability to tailor our code more closely towards specific requirements as we work with more complex data. It also gives us independence from the special controls provided with Web Matrix. On top of this, the same techniques are used with a whole range of standard Web Forms list controls, not just the DataGrid. There are list controls that provide more freedom to control the layout of the content, list controls that require far less processing overhead than the MxDataGrid, and controls that are specialized to provide things like HTML <select> lists, lists of radio buttons, and lists of checkboxes.

Interestingly, we can still use the SqlDataSourceControl with the other .NET Framework list controls if we wish, though there is little real advantage in doing so. All the standard list controls we'll be looking at in this chapter can use server-side data binding to a data source, through code that we write (or which, to some extent, Web Matrix writes for us).




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net