Building a DataGrid Helper Control For ASP.NET 1.x: Part 1
2004-09-13 12:40
666 查看
[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 typical data access page.
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 typical 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 restored but before the page is rendered. The DataGridHelper control exposes a single event 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:
In the next section, we will show how to use the DataGridHelper control.
|
相关文章推荐
- Building a DataGrid Helper Control for ASP.NET 1.x: Part 2
- Building a DataGrid Helper Control for ASP.NET 1.x: Part 3
- Google Maps Control for ASP.Net - Part 2 (转载)
- Server-side ASP .NET Data Binding, Part 2: Customizing the DataGrid Control
- Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
- ASP.NET 之 Chart Control for .Net Framework
- Implement Custom Paging in the ASP.Net Datagrid Control...
- 以服务器端为中心的 ASP.NET AJAX 模式 (Part 2 - Control)
- Flash Control for ASP.NET 2.0-Include Flash movies in your aspx pages
- jQuery EasyUI DataGrid 分页 FOR ASP.NET
- jQuery EasyUI DataGrid 分页 FOR ASP.NET
- PagerHelper for ASP.NET MVC3
- Building a custom data provider for .Text (a custom ASP.NET blogging application)
- 【翻译】Building a Simple Blog Engine with ASP.NET MVC and LINQ - Part 3
- 以服务器端为中心的 ASP.NET AJAX 模式 (Part 2 - Control)
- RadControl For Asp.net的官方API
- Interview Questions for ASP.NET Part - 1
- Custom TabControl for asp.net page
- [转]ASP.NET MVC - Creating a DropDownList helper for enums
- 在Codeproject.com上post了我的第一篇文章:Custom ComboBox server control for ASP.NET