因此,今天研究了一下DynamicDataControl控件,总结一下,文章的最后有源代码,大家如果有兴趣可以下下吧!
一、要用DynamicDataControl实现数据库连接,自然而然就想到了配置文件web.config
首先,在configSections节点添加名为dynamicDataControls的section节点
<configSections>
<section name="dynamicDataControls" type="Microsoft.Web.DynamicDataControls.DynamicDataControlsSection"/>
</configSections>
然后,添加数据库字符连接串:<section name="dynamicDataControls" type="Microsoft.Web.DynamicDataControls.DynamicDataControlsSection"/>
</configSections>
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=(local);Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>
接着,包含<dynamicDataControls>定义,且<dynamicDataControls>中声明了数据库连接字符串,那么动态数据控件将使用该数据库,这里以Northwind数据库为例:<add name="NorthwindConnectionString" connectionString="Data Source=(local);Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa"
providerName="System.Data.SqlClient" />
</connectionStrings>
<dynamicDataControls showAllTables="true" dataLayerType="Microsoft.Web.DynamicDataControls.SqlDataLayer"
connectionString="NorthwindConnectionString">
<nameMap>
<!--
By default, Dynamic Data Controls use the name of the table as the folder name. This
can be overridden by using a mapping here.
Sample mappings: -->
<add table="Employees" pathPrefix="~/MyNorthwind" />
</nameMap>
</dynamicDataControls>
<add table="Employees" pathPrefix="~/MyNorthwind" />connectionString="NorthwindConnectionString">
<nameMap>
<!--
By default, Dynamic Data Controls use the name of the table as the folder name. This
can be overridden by using a mapping here.
Sample mappings: -->
<add table="Employees" pathPrefix="~/MyNorthwind" />
</nameMap>
</dynamicDataControls>
这句怎么解释呢?它意思是在你的这个应用项目中的MyNorthwind目录中的文件都连接Employees表中;如果没有这句话的话,这必须让你的文件名为Employees表的名称,例如Employees.aspx才连接到Employees表中;若web.config中没有数据库连接字符串,但App_Data中包含了一个数据库文件,那么动态数据控件将使用该数据库。
二、再来看看DynamicDataControls控件的使用,其实很简单,只要拖放一两个控件出来而已,一个DynamicData控件,一个是asp.net服务器控件
具体各个控件组合如下:
DynamicAutoData(只需要一个控件而已!!!)
DynamicDetails + DetailsView
DynamicFilter + DropDownList(RadioButtonList,ListBox均可)
DynamicInsert + DetailsView
DynamicList + GridView
DynamicNavigator + Menu
DynamicRssLink + HyperLink
这样,只要拖出一两个控件,不需要写任何连接字符串以及去SQL表的代码,就能看到基本SQL功能的效果以及其他另人激动的效果。真的是相当地NB!
如果大家想看看效果是什么样的,那就下载该代码试试看吧!