Working with ObjectDataSource


ObjectDataSource is really a fun feature, in addition to being very useful. Instead of getting the data from any type of data storage, as would be the case with other DataSource controls, it simply uses business objects (classes that just store your application data). This is even more interesting if you work with the new Object-Spaces feature set included with Microsoft Visual Studio Whidbey Mobile Control through which objects are stored directly in the database.

Displaying Business Objects

So what do you need first of all to display business objects? Yes, some business objects! I've already created two for holding personal information in Listing 3-8: the Person class, with the Id, Firstname, and Lastname properties; and the PersonCollection class, which is based on Generics.

Listing 3-8: The Person and PersonCollection Classes

start example
 // Person.cs using System; public class Person {     private int id;     private string firstname;     private string lastname;     public Person(int id, string firstname, string lastname)     {         this.id = id;         this.firstname = firstname;         this.lastname = lastname;     }     public int Id     {         get { return this.id; }         set { this.id = value; }     }     public string Firstname     {         get { return this.firstname; }         set { this.firstname = value; }     }     public string Lastname     {         get { return this.lastname; }         set { this.lastname = value; }     } } // PersonCollection.cs using System; using System.Collections.Generic; public class PersonCollection : List<Person> {   public void Remove(int id)   {       Person person = this.FindPersonById(id);       if (person != null)       {           base.Remove(person);       }   }   public Person FindPersonById(int id)   {     foreach (Person person in this)     {         if (person.Id.Equals(id))         {             return person;         }     }     return null;   } } 
end example

The ObjectDataSource control expects to receive a qualified name of a class, which is responsible for processing the objects. Usually this is a class of your business layer. The first step of the implementation of a PersonManager class is shown in Listing 3-9. The SelectPersons() method creates a new PersonCollection, fills it with data, and returns it afterwards. The data is also stored in application scope. On consecutive calls, the data will be read from there.

Listing 3-9: Handling Application Logic with the PersonManager Class

start example
 using System; using System.Web; public class PersonManager {     private const string personsKey = "persons";     public PersonCollection SelectPersons()     {         HttpContext context = HttpContext.Current;     if (context.Application[personsKey] == null)     {         PersonCollection persons = new PersonCollection();         persons.Add(new Person(0, "Patrick", "Lorenz"));         persons.Add(new Person(1, "Micha", "Brunnhuber"));         persons.Add(new Person(2, "Thomas", "Ballmeier"));         persons.Add(new Person(3, "Marc", "H ppner"));         context.Application[personsKey] = persons;     }     return (context.Application[personsKey] as PersonCollection);   } } 
end example

OK, that's enough preparation. Now let's get to the meat. Add a GridView control to a new page and create a new ObjectDataSource control by using the wizard. Modify the properties of the new source and assign PersonManager to TypeName and SelectPersons to SelectMethod. Ready, steady, go! Hit the F5 key and the result should look like what you see in Figure 3-22.

click to expand
Figure 3-22: You can connect business objects directly to any Data Control.

Tip

Of course, the data source, or rather the GridView control, allows you to define explicitly the properties that will be visualized including their order. Just disable the automatic column generation and define them manually, as usual.

Using Parameters

The use of the ObjectDataSource control combined with parameters is pretty interesting. You can assign them to the data source and specify the data type to use as shown earlier and demonstrated in Figure 3-23. Here, I'll talk about SelectParameters.

click to expand
Figure 3-23: Use parameters to select what data you want displayed.

The defined parameters are passed to the select method. Unique names are required, and the data types must match. Overloading isn't supported. Be careful in the current Alpha version, which is case sensitive (this may change in the final release).

For this example, I have written a new method called SelectPerson (singular) which expects an ID as parameter (see Listing 3-10). The returned object is of type Person and gets bound to a DetailsView control. The ID is entered into a TextBox control, which serves as the source for the parameter. Figure 3-24 shows how to use it.

click to expand
Figure 3-24: Enter a valid ID to display the corresponding Person object.

Listing 3-10: Ensuring Parameter Names Match

start example
 // PersonManager.cs using System; using System.Web; public class PersonManager {     private const string personsKey = "persons";     public PersonCollection SelectPersons()     {        HttpContext context = HttpContext.Current;        if (context.Application[personsKey] == null)        {             PersonCollection persons = new PersonCollection();             persons.Add(new Person(0, "Patrick", "Lorenz"));             persons.Add(new Person(1, "Micha", "Brunnhuber"));             persons.Add(new Person(2, "Thomas", "Ballmeier"));             persons.Add(new Person(3, "Marc", "H ppner"));             context.Application[personsKey] = persons;         }         return (context.Application[personsKey] as PersonCollection);     }     public Person SelectPerson(int id)     {         return this.SelectPersons().FindPersonById(id);     } } // ObjectDataSource2.aspx <%@ page language="C#" %> <html> <head runat="server">     <title>Untitled Page</title> </head> <body>     <form runat="server">         ID: &nbsp;         <asp:textbox  runat="server" width="32px">0</asp:textbox>         <asp:button  runat="server" text="Show Person" />         <br />         <br />         <asp:detailsview  runat="server"             datasource>         </asp:detailsview>         <asp:objectdatasource  runat="server"             typename="PersonManager"             selectmethod="SelectPerson">             <selectparameters>                 <asp:controlparameter name="id"                     propertyname="Text"                     type="Int32"                     control>                 </asp:controlparameter>             </selectparameters>         </asp:objectdatasource>     </form> </body> </html> 
end example

Editing a SelectMethod and Deleting Business Objects

The features of the ObjectDataSource control don't stop at the visualization of data. It also provides editing and even deleting capabilities, if your business object allows this (see Figure 3-25). All you have to do is to add two more methods, Update and DeletePerson, as shown in Listing 3-11, and you're almost done. To get this example running, you have to assign the additional methods to the ObjectDataSource control and set the DataKeyNames property of the GridView control to Id.

click to expand
Figure 3-25: You can edit and delete your business object in a very direct manner.

Listing 3-11: Altering PersonManager to Support Editing and Deleting of Records in Memory

start example
 // PersonManager.cs using System; using System.Web; public class PersonManager {     private const string personsKey = "persons";     ...     public void DeletePerson(int Id)     {         HttpContext context = HttpContext.Current;         PersonCollection persons =             (context.Application[personsKey] as PersonCollection);         persons.Remove(Id);     } } // ObjectDataSource3.aspx <%@ page language="C#" %> <html> <head  runat="server">     <title>Untitled Page</title> </head> <body>     <form  runat="server">         <asp:gridview  runat="server"             datasource             autogeneratecolumns="False"             datakeynames="Id">             <columnfields>                 <asp:boundfield datafield="Id" readonly="True" headertext="ID">                 </asp:boundfield>                 <asp:boundfield datafield="Firstname" headertext="Firstname">                 </asp:boundfield>                 <asp:boundfield datafield="Lastname" headertext="Lastname">                 </asp:boundfield>                 <asp:commandfield showeditbutton="True">                 </asp:commandfield>                 <asp:commandfield showdeletebutton="True">                 </asp:commandfield>             </columnfields>         </asp:gridview>         <asp:objectdatasource  runat="server"             typename="PersonManager"             selectmethod="SelectPersons"             updatemethod="Update"             deletemethod="DeletePerson">         </asp:objectdatasource>     </form> </body> </html> 
end example

Adding Data to Business Objects

Last but not least, I should show you how to insert new data records. One solution is to use a combination of a GridView control and a DetailsView control as discussed previously. Because all parameters are returned as strings by the input fields, it's crucial to specify the adequate data types while defining the parameters.

Only a few changes are necessary. You need an additional DetailsView control, which you have to bind to the existing data source. The rest is done by the Insert method in the PersonManager class (see Listing 3-12). Inserting the record for a new person is shown in Figure 3-26.

click to expand
Figure 3-26: Of course, inserting new records is now supported as well.

Listing 3-12: Adding a New Person to the Collection

start example
 // PersonManager.cs using System; using System.Web; public class PersonManager {     private const string personsKey = "persons";     ...     public void Insert(int Id, string Firstname, string Lastname)     {         HttpContext context = HttpContext.Current;         PersonCollection persons =              (context.Application[personsKey] as PersonCollection);         persons.Add(new Person(Id, Firstname, Lastname));     } } // ObjectDataSource4.aspx     ...     <asp:objectdatasource                  runat="server"         typename="PersonManager"         selectmethod="SelectPersons"         deletemethod="DeletePerson"         updatemethod="Update"         insertmethod="Insert">         <insertparameters>              <asp:parameter name="Id" type="Int32"></asp:parameter>          </insertparameters>     </asp:objectdatasource>     <asp:detailsview          runat="server"         datasource         defaultmode="Insert"         autogeneraterows="False"         datakeynames="Id">         <rowfields>             <asp:boundfield datafield="Id" headertext="ID:">             </asp:boundfield>             <asp:boundfield datafield="Firstname" headertext="Fn:">             </asp:boundfield>             <asp:boundfield datafield="Lastname" headertext="Ln:">             </asp:boundfield>             <asp:commandfield showinsertbutton="True">             </asp:commandfield>         </rowfields>     </asp:detailsview> </form> </body> </html> 
end example




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

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