ObjectDataSource Controls can be used to represent any object. This tutorial will show you how we can use the ObjectDataSource control with collections and a DataGrid. C# version.
The Object Data Source Control can be used to represent any object. This tutorial will show how we can get started using the ObjectDataSource.
In this example, we will look at how we can create a list in a separate class and then use the ObjectDataSource to use this method in the class as the data source.
The first thing we will do is create our class and method. We will create a class called people, and a method that will create a collection of names. The method will make use of IEnumerable and look something like this:
| public List<string> GetPeople() { List<string> people = new List<string>(); } people.Add("Ryu"); people.Add("Alain"); people.Add("Katrina"); people.Add("Ali Babba"); people.Add("Jessica"); return people; |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
In a real-world situation, we probably wouldn't use a data source like this, but for this example, we will demonstrate how the ObjectDataSource can be used.
Because we are using a collection, we need to import the namespace. The entire class looks like this:
| using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Collections.Generic; /// <summary> /// Summary description for People /// </summary> public class People { public People() { // }// TODO: Add constructor logic here // public List<string> GetPeople() { List<string> people = new List<string>(); } } people.Add("Ryu"); people.Add("Alain"); people.Add("Katrina"); people.Add("Ali Babba"); people.Add("Jessica"); return people; |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
We are hard-coding some names to create a collection in the back-end. Now to use, this we will add an ObjectDataSource control to our ASPX page, as well as a DataGrid to read it:
| <form id="form1" runat="server"> <asp:DataGrid ID="DataGrid1" runat="server" /><br /> </form> <asp:Button ID="butGet" runat="server" Text="Get" onclick="butGet_Click" /> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="People" SelectMethod="GetPeople" /> |
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
Notice that our DataGrid's DataSourceID is assigned to that of the ObjectDataSource, which has a TypeName of People - this refers to our class, and the SelectMethod refers to our method within the class. We have also added a button with a click event to assign the ObjectDataSource to the DataGrid. This way we can see what happens. We will add the following code to the button:
| protected void butGet_Click(object sender, EventArgs e) { DataGrid1.DataSourceID = "ObjectDataSource1"; } DataGrid1.DataBind(); |
Now when we run this web application, we will be presented with a button, which when clicked, should display the DataGrid of the collection we created in the class.
No comments:
Post a Comment