|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Google Analytics ASP.NET Grid SerieThe Google Analytics ASP.NET Grid Serie will show you how to create an ASP.NET Grid that recreates the appearance and behavior of the Grid displayed in the Google Analytics web site.
IntroductionThe DataPager is one of the new webcontrols deployed with ASP.NET 3.5. The DataPager webcontrol allows you to add paging support for databound controls that implements the IPageableItemContainer interface. For example, you can use the DataPager webcontrol to give paging support to the new ListView webcontrol. One of the things that seems more interesting about the DataPager webcontrol is the ability to extend its functionality with a new one that fits your needs best. Extending the DataPager webcontrol is exactly what we are going to do now. We are going to create a custom DataPager that gives a similar functionality than the pager found in Google analytics (see Figure 1). ![]() Figure 1. Understanding the DataPager webcontrol and DataPagerFieldWhen you have a huge amount of data most of the times you don't want to display all the data at once. For performance and usability issues is better to display only a set of records and not all the records at once. The DataPager webcontrol allows you to split your data into pages and every time that a user select a page he will see the set of records corresponding to the selected page. For example, if you have 100 data records you can use the DataPager webcontrol to display only 10 records per page. The first page displays the records 1-10, the second page displays the records 11-20, and so on. To start using the DataPager webcontrol you only need to drag and drop a DataPager webcontrol to your webform. The DataPager webcontrol has two paging styles by default:
If you want to use the numeric style to page your data you need to add the NumericPagerField to the Fields collection of the DataPager webcontrol. <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1"> <Fields> <asp:NumericPagerField /> </Fields> </asp:DataPager> If you want to use the next and previous style to page your data you need to add the NextPreviousPagerField to the Fields collection of the DataPager webcontrol. <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1"> <Fields> <asp:NextPreviousPagerField /> </Fields> </asp:DataPager>The DataPager webcontrol actually doesn't render the appearance neither of the numeric style nor of the next and previous style. The NumericPagerField and NextPreviousPagerField classes handle the rendering of the numeric and the next and previous style, respectively. The NumericPagerField and the NextPreviousPagerField classes are known as navigation controls, both derive from the . If you want to create a custom appearance and functionality for the DataPager control then you can use the TemplatePagerField class or you can create a custom navigation control. For the purpose of this article we will create our own navigation control for the DataPager webcontrol. GooglePagerField: Google analytics navigation controlNow we are going to talk about our navigation control, the GooglePagerField. The GooglePagerField allows you to specify the first record to be displayed, set the maximum number of records displayed per page and move to the previous and next page.
Figure 4. Extending DataPagerFieldNow is time to create our Google data pager navigation control. We are not going to extend the DataPager class, what we are going to do is to extend the DataPagerField class. The DataPagerField class provides the basic functionality of a navigation control. I we want to extend the DataPagerField class we need to implement the functionality of the CreateField, CreateDataPagers and HandleEvent methods.public class GooglePagerField : DataPagerField
{
protected override DataPagerField CreateField()
{
}
public override void CreateDataPagers(DataPagerFieldItem container, int startRowIndex, int maximumRows,
int totalRowCount, int fieldIndex)
{
}
public override void HandleEvent(CommandEventArgs e)
{
}
}
CreateField methodReturns an instance of the GooglePagerField class.protected override DataPagerField CreateField()
{
return new GooglePagerField();
}
CreateDataPagers methodCreates the UI for our data pager. The table 1 explains the meaning of the parameters of the CreateDataPagers method.
Table 1.
The CreateDataPagers method creates the UI that will be displayed to the user. The CreateDataPagers method checks whether to render the UI using the values returned by the event raised by a user action or using the values passed in the query string of the ASPX webform.
public override void CreateDataPagers(DataPagerFieldItem container, int startRowIndex, int maximumRows,
int totalRowCount, int fieldIndex)
{
this._startRowIndex = startRowIndex;
this._maximumRows = maximumRows;
this._totalRowCount = totalRowCount;
if (string.IsNullOrEmpty(base.DataPager.QueryStringField))
{
this.CreateDataPagersForCommand(container, fieldIndex);
}
else
{
this.CreateDataPagersForQueryString(container, fieldIndex);
}
}
The CreateDataPagersForCommand method creates the controls used to give the functionality to our GooglePagerField.
private void CreateDataPagersForCommand(DataPagerFieldItem container, int fieldIndex)
{
//Goto item texbox
this.CreateGoToTexBox(container);
//Control used to set the page size.
this.CreatePageSizeControl(container);
//Set of records - total records
this.CreateLabelRecordControl(container);
//Previous button
if (this._showPreviousPage)
{
container.Controls.Add(this.CreateControl("Prev", this.PreviousPageText, fieldIndex,
this.PreviousPageImageUrl, this._showPreviousPage));
this.AddNonBreakingSpace(container);
}
//Next button
if (this._showNextPage)
{
container.Controls.Add(this.CreateControl("Next", this.NextPageText, fieldIndex,
this.NextPageImageUrl, this._showNextPage));
this.AddNonBreakingSpace(container);
}
}
HandleEvent methodHandles the events raised by the objects created in the CreateDataPagers method. The table 3 shows you the commands handled by the HandleEvent method.
public override void HandleEvent(CommandEventArgs e) { if (string.Equals(e.CommandName, "UpdatePageSize")) { base.DataPager.PageSize = Int32.Parse(e.CommandArgument.ToString()); base.DataPager.SetPageProperties(this._startRowIndex, base.DataPager.PageSize, true); return; } if (string.Equals(e.CommandName, "GoToItem")) { int newStartRowIndex = Int32.Parse(e.CommandArgument.ToString()); base.DataPager.SetPageProperties(newStartRowIndex, base.DataPager.PageSize, true); return; } if (string.IsNullOrEmpty(base.DataPager.QueryStringField)) { if (string.Equals(e.CommandName, "Prev")) { int startRowIndex = this._startRowIndex - base.DataPager.PageSize; if (startRowIndex < 0) { startRowIndex = 0; } base.DataPager.SetPageProperties(startRowIndex, base.DataPager.PageSize, true); } else if (string.Equals(e.CommandName, "Next")) { int nextStartRowIndex = this._startRowIndex + base.DataPager.PageSize; if (nextStartRowIndex > this._totalRowCount) nextStartRowIndex = this._totalRowCount - base.DataPager.PageSize; if (nextStartRowIndex < 0) nextStartRowIndex = 0; base.DataPager.SetPageProperties(nextStartRowIndex, base.DataPager.PageSize, true); } } } ButtonDropDownList webcontrolThe ButtonDropDownList is a custom dropdownlist webcontrol created to allow the user to select the page size. The ButtonDropDownList webcontrol implements the IPostBackEventHandler. The IPostBackEventHandler allows to the ButtonDropDownList webcontrol to raise a Command event which can be handled by the GooglePagerField class.public class ButtonDropDownList : DropDownList, IPostBackEventHandler { }The RaisePostBackEvent method raises the command event which is handled by the HandledEvent method of the GoogleDataPagerField class. The selected page size is passed as the event argument. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) { this.CommandArgument = "0"; if (base.SelectedItem != null) this.CommandArgument = this.SelectedItem.Value; this.RaisePostBackEvent(eventArgument); } protected virtual void RaisePostBackEvent(string eventArgument) { if (this.CausesValidation) { this.Page.Validate(this.ValidationGroup); } this.OnCommand(new CommandEventArgs(this.CommandName, this.CommandArgument)); } protected virtual void OnCommand(CommandEventArgs e) { CommandEventHandler handler = (CommandEventHandler)base.Events[EventCommand]; if (handler != null) { handler(this, e); } //It bubbles the event to the HandleEvent method of the GoogleDataPagerField class. base.RaiseBubbleEvent(this, e); } ButtonTextBox webcontrolThe ButtonTextBox is a custom textbox webcontrol created to allow the user to display the data records beginning at the specified record. The ButtonTextBox webcontrol implements the IPostBackEventHandler. The IPostBackEventHandler allows to the ButtonTextBox webcontrol to raise a Command event which can be handled by the GooglePagerField class.public class ButtonTextBox : TextBox, IPostBackEventHandler { }The RaisePostBackEvent method raises the command event which is handled by the HandledEvent method of the GoogleDataPagerField class. The specified record is passed as the event argument. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) { this.CommandArgument = base.Text; this.RaisePostBackEvent(eventArgument); } Using the GoogleDataPagerFieldTo use the Google data pager in your web applications you only need to add a GoogleDataPagerField to the fields collection of the DataPager class.<asp:ListView ID="ListView1" runat="server" DataSourceID="AccessDataSource1" ItemPlaceholderID="itemPlaceHolder"> <LayoutTemplate> <table class="tableInfo"> <tr> <th>ID</th> <th>First name</th> <th>Last name</th> </tr> <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder> <tr> <td colspan="3"> <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="5" > <Fields> <SqlNetFrameworkWebControls:GooglePagerField NextPageImageUrl="~/Images/button_arrow_right.gif" PreviousPageImageUrl="~/Images/button_arrow_left.gif" /> </Fields> </asp:DataPager> </td> </tr> </table> </LayoutTemplate> <ItemTemplate> <tr> <td><%# Eval("PlayerId") %></td> <td><%# Eval("FirstName") %></td> <td><%# Eval("LastName") %></td> </tr> </ItemTemplate> </asp:ListView> Version History
ConclusionThe DataPager webcontrol separates the paging functionality from the databound webcontrol. It allows you to have the freedom to customize the paging appearance and functionality to your own needs.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||