Building a DataGrid Helper Control for ASP.NET 1.x: Part 1

xiaoxiao2021-03-06  45

[Download Code] The DataGrid control in ASP.NET 1.x is a very powerful control. However, it still requires the understanding of the life cycle of ASP.NET as well as many lines of code to create a functional data access page. Much of coding related to DataGrid control is repetitive in nature. The upcoming ASP.NET 2.0 has a new GridView control and several data source controls. Using the GridView control and one of the data source controls, it is possible to build a data access page without writing a single line of code. in this series of articles, we will develop a custom control that will drastically simplify the DataGrid programming in ASP.NET 1.x today. in the first installment of this series, we will discuss how to simplify The Programming of a Read-Only Data Access Page.

In this article, we will first examine a typical data access page to identity the code that we can simplify. Next, we will build a control that allows us to simplify the DataGrid programming. We will then show how to use this control and conclude this Article.

NOW, Let us Take a Look of a typepical data access page.

A Typical Read-Only Data Access Page [Download Code] We will first look at what is involved in creating a typical data access page with sorting and paging functionality by examining an example A live demo can be found at http:. // www. DotNeteer.com/projects/datagridhelper/v1/typicaldataaccesspage.aspx. The Following Are The Basic Steps Used To Create The Page:

Create a SqlConnection and a SqlDataAdapter member: Drag a table from the Server Explorer into the WebForm design surface This will create a SqlConnection and a SqlDataAdapter member in the web form Give these members names that make sense This is one of the quickest ways... to create a SqlDataAdapter with simple select, insert, delete and update capabilities. In this article, we will only use the select capability. In the next article of this series, we will also make use the delete and update capabilities. Add a DataGrid Control :. Drag a DataGrid control from the tool box into the WebForm design surface enable sorting by setting the AllowSorting property to True and enable paging by setting the AllowPaging property to True Add code to respond to the Page_Load, DataGrid SortCommand and DataGridProducts PageIndexChanged events.. The Code Behind The Page Will Look Like: Private Void Page_Load (Object Sender, System.Eventargs E)

{

IF (! this.ispostback)

{

Bindgrid ();

}

}

Private void bindgrid ()

{

DataSet DS = New DataSet ();

SqlDataAdapterProducts.Fill (DS);

DataView DV = DS.TABLES [0] .defaultview;

Dv.Sort = (String) this.viewState ["sortexpression"];

DataGridProducts.DataSource = DV;

DataGridProducts.DATABIND ();

}

Private void DataGridProducts_SortCommand (Object Source,

System.Web.ui.WebControls.DataGridsortCommandEventArgs E)

{

String sortexpression = e.sortexpression;

// Alternate the Sorting Direction

IF (String) THIS.VIEWSTATE ["sortexpression"] == sortexpression)

sortexpression = "DESC";

THIS.VIEWSTATE ["sortexpression"] = sortexpression;

// Reset CurrentPageIndex on Resort

DataGridProducts.currentPageIndex = 0;

Bindgrid ();

}

Private void DataGridProducts_pageIndexchanged (Object Source,

System.Web.ui.WebControls.DataGridPageChangeDeventargs E)

{

DataGridProducts.currentPageIndex = E.NEWPAGEINDEX;

Bindgrid ();

} The bindGrid function is the function that creates the data source and binds it to the DataGrid. The code in the Page_Load event will populate the DataGrid when the page is initially accessed. The line, if (! This.IsPostBack), will prevent the bindGrid function from getting called multiple times in a post-back situation such as sorting or paging. The code in the SortCommand and PageIndexChanged event handlers are responsible for sorting and paging the data. in the code, we also added the functionality alternate the sort direction . on subsequent click of the same column header Note that the code in the three event handlers is rather generic; the knowledge of the data source is fully contained in the bindGrid function Therefore, the repetitive code in these three event handlers could be reused by. Moving it to a control. We will Examine How to do this in the next section.

Create a DataGrid Helper Control [Download Code] There are several methods to enhance the functionality of an existing control: to inherit from the control, to create a wrapper for the control or write an extender or helper class We will use the last method because. it preserves the design-time functionality of the DataGrid control. The DataGridHelper.cs file in the WebDataControls project contains the code for the DataGridHelper control. The DataGridHelper control has a DataGrid property that can be used to attach a DataGridHelper control to a DataGrid control. The DataGridHelper then handles the SortCommand and IndexChanged events of the DataGrid so that users of the control do not have to handle these events. The DataGridHelper also handled the Page_Load type functionality in the OnPreRender event of the control. We use the OnPreRender event because it fires After the controls states are already restred but before the point is rendered. The DataGridHelper Control Exposed A Single Eve nt called LoadData The users of DataGridHelper must respond to this event to get the data source and set it to the DataSource property of the event argument The DataGridHelper control also has a few additional properties that allow fine tuning its behavior..:

PropertyPurposeAlternateSortDirectionAlternate the sort direction on subsequent clicks of the same column headerAutoSortDataViewIf this property is true and the data source is a DataSet, DataTable or DataView, the DataGridHelper control will sort the data automaticallySortExpressionThe current sort expression. This property is useful when the user of the control wants to sort the data themselves in the LoadData event.ResetPageIndexOnSortingIf this property is True, the control will reset the page index whenever the sort expression is changed The DataGridHelper control also exposes a method, GridLoad. We need to call this method only if we have a post-back from another control and we want to refresh the data in response to the change. in the next section, we will show how to use the DataGridHelper control.Using the DataGridHelper Control [Download Code] The DataGridHelper.aspx page demonstrates the Use of datagridhelper control. a live demo can be subjectssed at http://www.dotneteer.com/pro jects / DataGridHelper / v1 / DataGridHelperTest.aspx. This page has the identical functionality as the TypicalDataAccessPage.aspx. It was also created in nearly the same way. The only difference is that we drop the DataGridHelper control into the page instead of writing all the code. One of the easiest ways to drop the control is to right click on one of the tabs in the tool box and click "Add / Remove items ..." from the dropdown menu. Then browse and select the WebDataControl.dll file that is in . the download that accompanies this article We then need to add some code to respond to the Page_Init event and the LoadData event of the DataGridHelper control The following shows the code behind:. private void DataGridHelper_LoadData (object Sender,

DotNeteer.WebDataControls.LoadDataEventArgs E) {

DataSet DS = New Dataset ();

SqlDataAdapterProducts.Fill (DS);

E.DataSource = DS;

}

Private void DataGridhelperTest_init (Object Sender, System.EventArgs E)

{

DataGridHelper.DataGrid = DataGridProducts;

} In the Init event, we need a single line of code to attach the DataGrid control to the DataGridHelper control. In the LoadData event, we create the data source and assign it to the DataSource property of the event argument. That is it! We Have a Functional Page. in The Next Section, WE WILL FURTHER Discuss The Features of DataGridHelper Control and contrude this article.

Final Notes [Download Code] in Concluding this Article, I Would Like to Point Out The Following:

The LoadData event only requires a data source to be created and assign to the DataSource property of the event argument. Therefore, it is possible to create the data source in a middle-tier class instead of in the data access page.

转载请注明原文地址:https://www.9cbs.com/read-117611.html

New Post(0)