日期:2014-03-07  浏览次数:20404 次

ASP.NET的出现,使网络程序员们设计程序的时候完全找到了“设计程序”的感觉,当然,更多的,他们感觉到了ASP.NET的得心应手。但是,没有想偷懒就没有进步,如果你仅仅依靠ASP.NET自己的强大功能而不想其他手段,那你很快就会发现别人设计程序比你会快很多而且轻轻松松。现在,我们来学习几招偷懒的手段,让别人跟在你后面敬佩吧。
使用过ASP的朋友一定都记得,ASP的很多功能需要一些第三方的组件实现,比如文件上传功能的实现就往往使用ASPCNUP组件实现。使用这些组件,不但可以扩展ASP程序的功能,而且,大大提高程序开发速度。我们这里介绍的偷懒手段,也就是介绍几款与我们平时设计密切相关的组件。
一、超级数据表格:SuperDataGrid
ASP.NET自带的DatGrid功能强大,定制也很方便,但是,因为它不是专门为数据库应用设计的。所以,在连接数据库的时候,我们不得不首先连接数据库,然后绑定数据。而SuperDataGrid是专门为数据库设计的,所以,那些繁琐的连接数据库我们也就没有必要去写了。需要
SuperDataGrid将DataGrid的一些属性简单化,使用这个控件,我们可以方便的实现数据库数据的显示、排序、修改数据,这些功能的实现,只要简单几行代码就可以。我们现在来看它的使用。
一)显示数据表
以下代码演示怎样使用SuperDataGrid来简单的显示数据表中的所有数据:
<%@ Register TagPrefix="Super" Namespace="Superexpert.Data"
Assembly="Superexpert.SuperDataGrid" %>

<Super:SuperDataGrid
ConnectionString="Server=localhost;UID=demo;pwd=secret;database=pubs"
TableName="Titles"
Runat="Server" />
具体效果请看:
http://www.superexpertcontrols.com/superdatagrid/samples/sample1.aspx
现在,我们来简单分析以上代码。第一行使调用SuperDataGrid控件,我们在以后的举例中都将使用到。第二行,和标准的DataGrid使用差不多,我们看看SuperDataGrid的一些属性:
ConnectionString:因为是连接数据库,当然少不了数据库连接语句。这个参数就是连接数据的语句;
TableName:要显示具体的表,我们就在这里定义。
看到这里,我们已经感觉到了“简单”,但是,在实际的应用中,像这种直接显示一个表的情况是很少的。所以,我们需要其他更多的功能。最直接的,我们需要Select语句的返回结果。
<%@ Register TagPrefix="Super" Namespace="Superexpert.Data"
Assembly="Superexpert.SuperDataGrid" %>

<Super:SuperDataGrid
ConnectionString="Server=localhost;UID=sa;pwd=secret;database=Northwind"
CommandText="Select ProductName, CategoryName  
From Products, Categories Where Products.CategoryID=Categories.CategoryID"
Runat="Server" />
具体效果请看:
http://www.superexpertcontrols.com/superdatagrid/samples/sample2.aspx
以上代码返回Select语句的结果。在这里,我们见到一个新的属性:
CommandText:和Command一样,就是Select语句;
二)数据排序
在DataGrid中,数据排序虽然简单,但是代码还是不少。我们现在来看SuperDataGrid中怎样给数据排序:
<%@ Register TagPrefix="Super" Namespace="Superexpert.Data"
Assembly="Superexpert.SuperDataGrid" %>

<form runat="Server">
<Super:SuperDataGrid
ConnectionString="Server=localhost;UID=sa;pwd=secret;database=Pubs"
TableName="Titles"
EnableSorting="True"  
Runat="Server" />  
</form>
具体效果请看:
http://www.superexpertcontrols.com/superdatagrid/samples/sample3.aspx
仔细看以上代码,其实就是设置了一个EnableSortinga属性为真。也就是打开排序功能。需要仔细注意的一点,要将SuperDataGrid包括在Form中。
三)数据分页
在ASP中,很多朋友会为分页烦恼,现在,我们看看SuperDataGrid中怎样分页:
<%@ Register TagPrefix="Super" Namespace="Superexpert.Data"
Assembly="Superexpert.SuperDataGrid" %>

<form runat="Server">
<Super:SuperDataGrid
ConnectionString="Server=localhost;UID=sa;pwd=secret;database=pubs"
TableName="Titles"
EnablePaging="True"
PageSize="3"
PagerStyle-Mode="NumericPages"
Runat="Server" />  
</form>
具体效果请看:
http://www.superexpertcontrols.com/superdatagrid/samples/sample4.aspx
我们来看看SuperDataGrid的几个新属性:
EnablePaging:首先,我们当然要打开数据分页;
PageSize:和DataGrid一样,每页数据显示的条数;
PagerStyle-Mode:和DataGrid一样,页码显示方式;
四)数据编辑
我们知道,在DataGrid中,我们可以在直接编辑数据,但是,一般我们很少使用这样功能,因为这样编辑数据不是很方便也不是很实用,代码编写也比较多。现在,SuperDataGrid也提供这个功能,当然,我们不需要写那么多代码,只需要简单的设置就可以,其他,SuperDataGrid全部帮我们弄好了。
<%@ Register TagPrefix="Super" Namespace="Superexpert.Data"
Assembly="Superexpert.SuperDataGrid" %>

<form runat="Server">
<Super:SuperDataGrid
ConnectionString="Server=localhost;UID=sa;pwd=secret;database=Northwind"
TableName="Products"
EnableEditing="True"
EnablePaging="True"
Runat="Server" />  
</form>
具体效果请看:
http://www.superexpertcontrols.com/superdatagrid/samples/sample5.aspx
看以上代码,如果需要编辑数据,只要加EnableEditing属性就可以了。是不是特别简单?当然,我们仍然要将SuperDataGrid放在Form中。
五)缓存
ASP.NET的缓存功能我们已经知道很强大,但是,具体到SuperDataGrid,你会发现它更加方便。使用SuperDataGr