ObjectdataSource, SQL-Server and ASP.NET Ajax-Extensions |
This tutorial is about using the ObjectDataSource in combination of ASP.NET, Ajax-Extensions and SQL-Server-database.The ObjectDataSource is shipped with .NET-framework 2.0. |
|
|
|
The example |
We use an ObjectDataSource on an ASP.NET-Form for displaying a list of Person-objects in a GridView. The data is stored in an SQL-Server-database, to access these data a particular manager-class (PersonManager) is defined with a method that returns a generic-list of Person-objects. The result can be filtered by name which can be typed in a TextBox. The GridView is placed inside of an UpdatePanel so that only this part of the page is updated after filtering the results. |
|
The database
|
 |
| Figure 1 |
The primary key is id_person which has a defaultvalue of newid(),which generates a new guid on every insert into the table. |
The business-object Person |
A Person object is defined like that: |
|
public class Person
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
private string _street;
public string Street
{
get
{
return _street;
} set
{
_street = value;
}
}
private string _zip;
public string Zip
{
get { return _zip;
} set
{
_zip = value;
}
}
private string _phoneno;
public string Phoneno
{
get
{
return _phoneno;
}
set
{
_phoneno = value;
}
}
public Person(IDataReader reader)
{
_name = reader["name"].ToString();
_street = reader["street"].ToString();
_zip = reader["zip"].ToString();
_phoneno = reader["phoneno"].ToString();
}
} |
See the definition of the constructor: The object is filled by an opened IDataReader which is opened by the class PersonManager.
|
| Accessing Person-objects |
public class PersonManager
{
private string _conString;
public PersonManager()
{
_conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
}
public IList GetPersons(string nameFilter)
{
string sql = "SELECT * FROM Person ";
if (nameFilter != "")
{
sql += "WHERE name like '" + nameFilter + "%' ";
}
sql +="ORDER BY name";
SqlConnection connection = null;
IDataReader reader = null;
IList result = new List();
try
{
connection = new SqlConnection(_conString);
connection.Open();
IDbCommand cmd = connection.CreateCommand();
cmd.CommandText = sql;
reader = cmd.ExecuteReader();
while (reader.Read())
{
result.Add(new Person(reader));
}
}
catch (Exception ex)
{
}
finally
{
if (reader != null)
reader.Close();
if (connection != null)
connection.Close();
}
return result;
}
} |
We have the possibility to select a range of Person-objects by the method GetPersons. The persons are filtered by their names (starting-letters). This is the only code we need to write in our project, the business-logic. For selecting and filtering persons we use the ObjectDataSource. |
| Using the ObjectDataSource |
The ObjectDataSource is a class which is available since .NET 2.0. VS 2005 offers a nice integration of this datasource when you open the wizard for configuring datasources. Just drag an ObjectDataSource to your aspx-form: |
|
| Figure 2 |
You can choose the class (PersonManager) from where to get the objects to bind to the ObjectDataSource. Then you select the method (GetPersons) which provides the IList of objects you want to bind. In our case, the method has a parameter (nameFilter) that we want to get from an ASP.NET-control (TextBox).After that we are ready to display and filter persons without writing code for this logic. |
| The Ajax-Part |
We don't want the whole page to be reloaded after we filtered the persons by name. To achieve this, we placed the GridView into an UpdatePanel and defined the TextBox for filtering as asynchronous Trigger for the UpdatePanel: |
|
| Figure 3 |
| Here is the whole markup of the UpdatePanel: |
<asp:UpdatePanel ID="updatePanelPersons" runat="server"> <ContentTemplate> <asp:GridView ID="gridPersons" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="450px" AutoGenerateColumns="False" DataSourceID="objDsPersons" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Street" HeaderText="Street" SortExpression="Street" />
<asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
<asp:BoundField DataField="Phoneno" HeaderText="Phoneno" SortExpression="Phoneno" />
</Columns >
</asp:GridView >
</ContentTemplate >
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtName" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</html> |
When the user is typing the beginning-letters of a person's name and presses return, the GridView is filtered because it is bound to the ObjectDataSource. Only the GridView is updated and not the whole page, because the GridView is placed inside of an UpdatePanel that is triggered by the event TextChanged of the TextBox. |