In the last
introductory part, I introduced a little to those who were not familiar with the GridView element intended to display tabular information on a form. I told you that GridView (for my convenience, I will call this element in the following everywhere as a gridview) can be associated with a data source. Sources can be several types. In my examples, ObjectDataSource will be used as a source everywhere.
So, as I said, use an ObjectDataSource. This part will be dedicated to him. Below we define methods that return data, add strokes that will help us in the future when developing from the visual environment, and also repeat all the actions, but this time through the codebehind page.
')
We agree that for development we use VS 2008. We open a studio, create a new project like ASP.NET WebSite, the development language is C #. Open the Default.aspx page in Split or Design mode. We throw on the form gridview. We click on the arrow in the upper right corner of the gridview, select the new data source. The source type is Object. Go ahead and see the form where we are asked to choose a business object.

Since we have no objects so far, we will not be able to choose anything. Therefore, we proceed to the creation of our business facility.
In order to speed up the development of objects in the future, we will create an abstract class. This class will contain at least two public methods common to all the ancestors of our class. The first one — let's call it Select — will return us an array of objects, the second one — let's call it SelectCount — will return the number of all elements that satisfy the query. Let's not forget propping, therefore, as the required parameters for the Select method, we specify two input parameters: int maximumRows, int startRowIndex. These default names are used by the ObjectDataSource to pass to the method the number of rows to fetch and the index from which to start the selection. We do not specify any parameters for the SelectCount method.
[System.ComponentModel.DataObject]
public abstract class BusinessObject<T>
{
protected T[] _objs;
private int maximumRows, startRowIndex;
[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Select, true )]
public virtual T[] Select( int maximumRows, int startRowIndex)
{
this .maximumRows = maximumRows;
this .startRowIndex = startRowIndex;
List <T> arr = new List <T>();
int to = startRowIndex + maximumRows;
if (to > _objs.Length)
to = _objs.Length;
for ( int i = startRowIndex; i < to; i++)
{
arr.Add(_objs[i]);
}
return arr.ToArray();
}
public int SelectCount()
{
return _objs.Length;
}
}
* This source code was highlighted with Source Code Highlighter .
Let's review the class in more detail. What is after the class name, I will not explain. Ignorant can google about generic. Inside the class, the _objs type T array array is declared. By default, it turns out that inside each class of the heir we will have a variable containing the type we specify (we will look further). Until I explain to what the attributes of the class header and the Select method are for us, I will just say that they do not affect the code in any way, and it is quite possible to do without them.
The Select method is virtual to be able to override it in the descendant classes. The SelectCount method returns the total number of elements for the Select method. In my project, I agreed that for each select that returns an array of objects, there is a method that returns the number of records. The name of this method always consists of the name of the select method plus the Count suffix. Those. if we have a SelectById method, then there must be a SelectByIdCount method for it. Attentive ones might object to me that, for the SelectById method, there is no need to return the number of elements, because this method will always return only one element. Therefore, in this case, we can not define the SelectByIdCount method.
Go ahead, create our business class based on the abstract class BusinesObject.
[System.ComponentModel.DataObject]
public class SimpleBusinesObject:BusinessObject<Person>
{
private int cnt = 0;
public SimpleBusinesObject()
{
List <Person> p = new List <Person>();
for ( int i = 0; i < 24; i++)
{
p.Add( new Person( "John " + i.ToString(), i, "Lenina str., " + i.ToString()));
}
p.Add( new Person( "Misha" , "123-4567" , "Kremlin" ));
_objs = p.ToArray();
}
[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Select, false )]
public Person[] SelectByName( int maximumRows, int startRowIndex, String name)
{
List <Person> p = new List <Person>();
for ( int i = 0; i < _objs.Length; i++)
{
if (_objs[i].Name.IndexOf(name))
p.Add(_objs[i]);
}
cnt = p.Count;
return p.GetRange(startRowIndex, maximumRows).ToArray();
}
public int SelectByNameCount( String name)
{
return cnt;
}
}
* This source code was highlighted with Source Code Highlighter .
Person class:
public class Person
{
private String _name;
private String _phone;
private String _address;
public String Name
{
get { return _name; }
set { _name = value ; }
}
public String Phone
{
get { return _phone; }
set { _phone = value ; }
}
public String Address
{
get { return _address; }
set { _address = value ; }
}
public Person()
{
_name = "No Name" ;
_phone = "555-5555" ;
_address = " " ;
}
public Person( String name, String phone, String address)
{
_name = name;
_phone = phone;
_address = address;
}
}
* This source code was highlighted with Source Code Highlighter .
Now you can do our page default.aspx

.
As you can see in the screenshot, we can select our created business object SimpleBusinessObject. To the right of the list with a choice of business objects is ticked Show only data components. What it is, why it is needed. I'll explain now. Recall which attribute we specified in our class: [System.ComponentModel.DataObject]. With this attribute we mark the classes that we use to access the data. Imagine. If hundreds and more different classes were defined in our project, it would be difficult to find the object we need. And the use of this attribute together with the tick set filters only those classes. Which we need. On the following form, after choosing our business facility, we are asked to choose a method that will be used for the select. Here again the [System.ComponentModel.DataObjectMethodAttribute (System.ComponentModel.DataObjectMethodType.Select, true)] attribute will help us
With which we mark our methods. Return data. In the end, you will notice that we have true, it means that this method will be immediately selected in the list of methods. Therefore, it makes sense to set one of the methods to true if it will be used more often than others. So, for the Select method it is true, and for the SelectByName method it is false. So choose the first method in the list, go ahead, we see that we have a list of parameters. Here we will not change anything yet, click pimpu, ready. Now look at our generated html code:
< asp:GridView ID ="GridView1"
runat ="server"
AllowPaging ="True"
PageSize ="10"
AutoGenerateColumns ="False"
DataSourceID ="ObjectDataSource1" >
< Columns >
< asp:BoundField DataField ="Name" HeaderText ="Name" SortExpression ="Name" />
< asp:BoundField DataField ="Phone" HeaderText ="Phone" SortExpression ="Phone" />
< asp:BoundField DataField ="Address" HeaderText ="Address"
SortExpression ="Address" />
</ Columns >
</ asp:GridView >
< asp:ObjectDataSource ID ="ObjectDataSource1" runat ="server"
SelectMethod ="Select"
SelectCountMethod ="SelectCount"
EnablePaging ="true"
TypeName ="SimpleBusinesObject" >
</ asp:ObjectDataSource >
* This source code was highlighted with Source Code Highlighter .
I have already corrected everything that is needed here, organized paging. I note that if we want a paging that we created manually (the choice is exactly what is needed and how much is needed, without filtering the necessary elements by means of asp.net), then to hell we throw out all the parameters from the ObjectDataSource (more precisely, not all but only two, maximumRows and startRowIndex)! This is a very important note. For here there is a lot of any problems, the removal of the brain, etc. Once again, I note:
If paging is organized by hand:
- We throw out two fun parameters, maximumRows and startRowIndex, leaving only those that specify additional filtering.
- These parameters are not specified in the SelectCount method, we leave only those that specify additional filtering.
- Add attributes to GridView: AllowPaging = true, PageSize = how many
- Add SelectCountMethod = method_name, EnablePaging = true attributes to ObjectDataSource
We start our project and see that everything works fine.
Now we will display only the list of those persons who have a certain substring in the name
< asp:GridView ID ="GridView2" runat ="server"
AllowPaging ="True"
PageSize ="10"
AutoGenerateColumns ="False"
DataSourceID ="ObjectDataSource2" >
< Columns >
< asp:BoundField DataField ="Name" HeaderText ="Name" SortExpression ="Name" />
< asp:BoundField DataField ="Phone" HeaderText ="Phone" SortExpression ="Phone" />
< asp:BoundField DataField ="Address" HeaderText ="Address"
SortExpression ="Address" />
</ Columns >
</ asp:GridView >
< asp:ObjectDataSource ID ="ObjectDataSource2" runat ="server"
SelectMethod ="SelectByName"
SelectCountMethod ="SelectByNameCount"
EnablePaging ="true"
TypeName ="SimpleBusinesObject" >
< SelectParameters >
< asp:QueryStringParameter DefaultValue ="John" Name ="name"
QueryStringField ="name" Type ="String" />
</ SelectParameters >
</ asp:ObjectDataSource >
* This source code was highlighted with Source Code Highlighter .
I already removed all the excess, added the necessary. Run, hooray! Works. But we note that we can’t open the last page, because “Those who want can correct this misunderstanding and make sure that at the end of the list we will not have a person named Misha. And now you can go to smoke (if you smoke, or drink some coffee), Imagine yourself as a cool programmer on asp.net and score for everything
So, we have considered a simple paging, filtered everything that we need. Now I can only make a few comments. If you use ORM, then it is quite possible that you will have to redo the sql query in the Select method so that the database gives the necessary portion of data. Also, for the SelectCount method, you will again have to remake the sql query so that it does not return anything except the number of elements that satisfy the query. Thus, you will significantly reduce the amount of data transferred between the database server and the web server.
I will take my leave behind this, and in the future, perhaps, we will sort the sorting, the insertion of new elements, the deletion and the update. But it will already be much easier, because we already have the basics of working with the GridView & ObjectDataSource.
And finally, we
attach the created project
files.mail.ru/4UKA8ZPS A. well, yes. I forgot, tomorrow we will set up the gridview and datasource from codebehind.