Scott Mitchell 的ASP.NET 2.0数据教程之四十:: 自定义DataList编辑界面

CollectionEditor集合编辑器使用   最近遇到一个项目需要使用的propertyGrid来让用户自定义属性,在遇到对集合进行操作的的时候遇到几个问题:     1.在集合编辑器上,属性设计区域下面的没有属性描述区域,导致有的用户无法知道这个属性有啥用,如下图            2.集合编辑器只能指定同一个类型,但有个需求是需要在一个集合里添加基于同一个基类的不同拓展的多个类型     3.需要限制集合编辑器Items的... 阅读详情

原文 | 下载本教程中的编码例子 | 下载本教程的PDF版

导言


DataList的编辑界面由EditItemTemplate里的标记语言和web控件定义。在目前为止所做的DataList编辑功能的例子里,编辑界面都只包含TextBox。在前面一章里,我们通过添加验证控件来增加了用户体验,提高了可用性。


EditItemTemplate可以包含除了TextBox以外的很多控件,比如DropDownLists, RadioButtonLists, Calendars等。和使用TextBox一样,使用这些控件自定义编辑界面时,步骤如下:

  1. 为EditItemTemplate添加控件.
  2. 使用绑定语法将相关的字段值赋给控件的属性.
  3. 在UpdateCommand事件处理里, 编程访问web控件的值,并将它传给相关的BLL的方法.


本章我们将为DataList创建一个更丰富的编辑界面,它将包含DropDownList和CheckBox。我们将创建一个列出product信息的DataList,用户可以更新它的name,supplier,category和discontinued status。见图1。

图 1: 编辑界面包含一个TextBox, 两个 DropDownLists和一个CheckBox

第一步: 显示Product 信息


在创建DataList的编辑界面前,我们需要先创建一个只读界面。先打开EditDeleteDataList文件夹下的CustomizedUI.aspx页,拖一个DataList进来,将ID设为Products。通过DataList的智能标签,创建一个名为ProductsDataSource的ObjectDataSource,用ProductsBLL类的GetProducts方法配置它。象前面一章一样,我们将直接通过BLL来更新product信息。在UPDATE,INSERT,DELETE标签里选择None.

图 2: 在UPDATE, INSERT, DELETE 标签的下拉列表里选择 (None)


配置完ObjectDataSource后,Visual Studio会自动创建默认的ItemTemplate,列出每个字段的值。将product name用<h4>表示,并添加一个Edit button,确保将它的CommandName属性设为 “Edit”. 我的标记语言如下:

ASP.NET
1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            
<ItemTemplate>
            <h4>
            <asp:Label ID="ProductNameLabel" runat="server"
            Text='<%# Eval("ProductName") %>' />
            </h4>
            <table border="0">
            <tr>
            <td class="ProductPropertyLabel">Category:</td>
            <td class="ProductPropertyValue">
            <asp:Label ID="CategoryNameLabel" runat="server"
            Text='<%# Eval("CategoryName") %>' />
            </td>
            <td class="ProductPropertyLabel">Supplier:</td>
            <td class="ProductPropertyValue">
            <asp:Label ID="SupplierNameLabel" runat="server"
            Text='<%# Eval("SupplierName") %>' />
            </td>
            </tr>
            <tr>
            <td class="ProductPropertyLabel">Discontinued:</td>
            <td class="ProductPropertyValue">
            <asp:Label ID="DiscontinuedLabel" runat="server"
            Text='<%# Eval("Discontinued") %>' />
            </td>
            <td class="ProductPropertyLabel">Price:</td>
            <td class="ProductPropertyValue">
            <asp:Label ID="UnitPriceLabel" runat="server"
            Text='<%# Eval("UnitPrice", "{0:C}") %>' />
            </td>
            </tr>
            <tr>
            <td colspan="4">
            <asp:Button runat="Server" ID="EditButton"
            Text="Edit" CommandName="Edit" />
            </td>
            </tr>
            </table>
            <br />
            </ItemTemplate>
            


上面的标记语言用<h4>表示product name,4列的<table>展示其它字段。前面已经讨论过Styles.css里定义的ProductPropertyLabel和productPropertyValue类。浏览该页,见图3。

图 3: 显示product信息

第二步: 为编辑界面添加web控件


首先向EditItemTemplate里添加需要的web控件。我们需要用一个DropDownList表示category,一个DropDownList表示supplier,一个CheckBox 表示discontinued state。由于本例中不用编辑price,所以仍然用Label来表示它。

点击DataList的智能标签上的“Edit Templates”,选择EditItemTemplate,为它添加一个ID为Categories的EditItemTemplate。

图 4: 为Categories添加一个DropDownList


然后从DropDownList的智能标签里选择“Choose Data Source”,创建一个名为CategoriesDataSource的ObjectDataSource。用CategoriesBLL类的GetCategories()方法配制它(见图5)。数据源配置向导会要求为ListItem Text和Value选择字段。让DropDownList 显示CategoryName,CategoryID作为Value,见图6。

图 5: 创建 ObjectDataSource

图 6: 配置DropDownList的 Display 字段和Value 字段

重复上面的步骤,为suppliers创建一个ID为Suppliers的DropDownList 和一个名为SuppliersDataSource的ObjectDataSource。

然后为discontinued state 添加一个CheckBox ,为name添加一个TextBox 。将他们的ID分别设为Discontinued和ProductName。为product name添加一个RequiredFieldValidator 确保用户必须提供这个值。

Lastly, add the Update and Cancel buttons. Remember that for these two buttons it is imperative that their CommandName properties are set to “Update” and “Cancel”, respectively.
最后添加Update 和Cancel button。记得这两个button的CommandName属性必须分别设为“Update” 和“Cancel”。

你可以将编辑界面以你喜欢的方式展示。我选择使用和只读界面一样的界面来显示,见下面的声明代码和截图。

ASP.NET
1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            36
            37
            38
            39
            40
            41
            42
            43
            44
            45
            46
            47
            48
            49
            50
            51
            52
            53
            54
            55
            56
            57
            58
            59
            60
            61
            62
            63
            
<EditItemTemplate>
            <h4>
            <asp:Label ID="ProductNameLabel" runat="server"
            Text='<%# Eval("ProductName") %>' />
            </h4>
            <table border="0">
            <tr>
            <td class="ProductPropertyLabel">Name:</td>
            <td colspan="3" class="ProductPropertyValue">
            <asp:TextBox runat="server" ID="ProductName" Width="90%" />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
            ControlToValidate="ProductName"
            ErrorMessage="You must enter a name for the product."
            runat="server">*</asp:RequiredFieldValidator>
            </td>
            </tr>
            <tr>
            <td class="ProductPropertyLabel">Category:</td>
            <td class="ProductPropertyValue">
            <asp:DropDownList ID="Categories" runat="server"
            DataSourceID="CategoriesDataSource"
            DataTextField="CategoryName" DataValueField="CategoryID" />
            </td>
            <td class="ProductPropertyLabel">Supplier:</td>
            <td class="ProductPropertyValue">
            <asp:DropDownList ID="Suppliers"
            DataSourceID="SuppliersDataSource" DataTextField="CompanyName"
            DataValueField="SupplierID" runat="server" />
            </td>
            </tr>
            <tr>
            <td class="ProductPropertyLabel">Discontinued:</td>
            <td class="ProductPropertyValue">
            <asp:CheckBox runat="server" id="Discontinued" />
            </td>
            <td class="ProductPropertyLabel">Price:</td>
            <td class="ProductPropertyValue">
            <asp:Label ID="UnitPriceLabel" runat="server"
            Text='<%# Eval("UnitPrice", "{0:C}") %>' />
            </td>
            </tr>
            <tr>
            <td colspan="4">
            <asp:Button runat="Server" ID="UpdateButton" CommandName="Update"
            Text="Update" />
            &nbsp;&nbsp;&nbsp;
            <asp:Button runat="Server" ID="CancelButton" CommandName="Cancel"
            Text="Cancel" CausesValidation="False" />
            </td>
            </tr>
            </table>
            <br />
            <asp:ObjectDataSource ID="CategoriesDataSource" runat="server"
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetCategories"
            TypeName="CategoriesBLL">
            </asp:ObjectDataSource>
            <asp:ObjectDataSource ID="SuppliersDataSource" runat="server"
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetSuppliers"
            TypeName="SuppliersBLL">
            </asp:ObjectDataSource>
            </EditItemTemplate>
            

图 : 编辑界面和只读界面的展示差不多

第三步: 创建 EditCommand和CancelCommand Event Handlers


现在在EditItemTemplate里除了UnitPriceLabel外还没有绑定语法(从ItemTemplate复制过来的代码)。在添加绑定语法前我们首先为DataList的EditCommand和CancelCommand创建事件处理。EditCommand事件处理的目标是为了将Edit button被点击的item展示为编辑状态,而CancelCommand的目标是将DataList返回到编辑前状态。

见下面的代码:

C#
1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            
protected void Products_EditCommand(object source, DataListCommandEventArgs e)
            {
            // Set the DataList's EditItemIndex property and rebind the data
            Products.EditItemIndex = e.Item.ItemIndex;
            Products.DataBind();
            }
            protected void Products_CancelCommand(object source, DataListCommandEventArgs e)
            {
            // Return to DataList to its pre-editing state
            Products.EditItemIndex = -1;
            Products.DataBind();
            }
            


完成这些后,点击Edit button会进入编辑界面,点击Cancel button会返回只读模式。见图8。由于现在还没有为编辑界面添加绑定语法,TextBox是空白的,CheckBox 未被选中,两个DropDownList里都是第一个item被选中。

图 : 点击Edit Button显示编辑界面

第四步: 为编辑界面增加绑定语法


为了让编辑界面显示当前product的值,我们需要使用绑定语法将字段的值赋给web控件。绑定语法可以通过选择web控件的智能标签的“Edit DataBindings”或者直接添加声明语法来实现。


将ProductName字段的值赋给ProductName TextBox的Text属性,CategoryID和SupplierID字段赋给Categories和Suppliers DropDownList的SelectedValue属性,Discontinued字段赋给Discontinued CheckBox的Checked属性。完成这些后,浏览页面并点击Edit button.见图9。

图 : 点击Edit Button 显示编辑界面

第五步: 在UpdateCommand Event Handler保存用户的更改


当用户编辑product并点Update button后,会postback并激发UpdateCommand事件。在事件处理里,我们需要从EditItemTemplate里读出web控件的值,并和BLL交互,然后更新数据库里的product。如我们在前面一章看到的那样,被更新的product的ProductID可以通过DataKeys集合来获取。用户输入的值可以通过FindControl("controlID")来编程获取,见下面的代码:

C#
1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17
            18
            19
            20
            21
            22
            23
            24
            25
            26
            27
            28
            29
            30
            31
            32
            33
            34
            35
            
protected void Products_UpdateCommand(object source, DataListCommandEventArgs e)
            {
            // Make sure the page is valid...
            if (!Page.IsValid)
            return;
            // Read in the ProductID from the DataKeys collection
            int productID = Convert.ToInt32(Products.DataKeys[e.Item.ItemIndex]);
            // Read in the product name and price values
            TextBox productName = (TextBox)e.Item.FindControl("ProductName");
            DropDownList categories = (DropDownList)e.Item.FindControl("Categories");
            DropDownList suppliers = (DropDownList)e.Item.FindControl("Suppliers");
            CheckBox discontinued = (CheckBox)e.Item.FindControl("Discontinued");
            string productNameValue = null;
            if (productName.Text.Trim().Length > 0)
            productNameValue = productName.Text.Trim();
            int categoryIDValue = Convert.ToInt32(categories.SelectedValue);
            int supplierIDValue = Convert.ToInt32(suppliers.SelectedValue);
            bool discontinuedValue = discontinued.Checked;
            // Call the ProductsBLL's UpdateProduct method...
            ProductsBLL productsAPI = new ProductsBLL();
            productsAPI.UpdateProduct(productNameValue, categoryIDValue, supplierIDValue,
            discontinuedValue, productID);
            // Revert the DataList back to its pre-editing state
            Products.EditItemIndex = -1;
            Products.DataBind();
            }
            


代码首先检查Page.IsValid属性来确保所有的验证控件都返回合法值。如果Page.IsValid为True,从DataKeys集合里读出被编辑的product 的ProductID的值,并引用EditItemTemplate里的web控件。然后将这些控件的值读到变量里,并传给UpdateProduct方法。完成更新后,DataList会返回到编辑前的状态。

and this example focused. As an exercise, add this functionality after completing this tutorial.
注意:我省略了某章异常处理,目的是为了使本章的代码看起来目的性更强。你可以在完成本章后自己添加异常处理的功能作为练习。

第六章: 处理空的CategoryID 和SupplierID 值


Northwind 数据库允许Products表里的CategoryID和SupplierID列为空。然而我们的编辑界面目前还没有提供可选空值。如果我们试图编辑一个无论是CategoryID还是SupplierID为空的product,将会产生ArgumentOutOfRangeException异常。目前我们也没有将product的category或supplier的值从一个非空值转换为空值的方法。

为了在DropDownLists里添加空值,我们需要添加一个ListItem。我将ListItem里的Text显示为"(None)",你可以将它赋为任何你希望的值(比如空字符串)。最后,记得将DropDownLists的AppendDataBoundItems设为True。如果你没有这么做,绑定到DropDownList 的categories 和suppliers 会被添加的ListItem覆盖。


完成这些后,DropDownLists的标记语言看起来应该和下面差不多:

ASP.NET
1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            
<asp:DropDownList ID="Categories" runat="server" DataSourceID="CategoriesDataSource"
            DataTextField="CategoryName" DataValueField="CategoryID"
            SelectedValue='<%# Eval("CategoryID") %>' AppendDataBoundItems="True">
            <asp:ListItem Value="" Selected="True">(None)</asp:ListItem>
            </asp:DropDownList>
            ...
            <asp:DropDownList ID="Suppliers" runat="server" DataSourceID="SuppliersDataSource"
            DataTextField="CompanyName" DataValueField="SupplierID"
            SelectedValue='<%# Eval("SupplierID") %>' AppendDataBoundItems="True">
            <asp:ListItem Value="" Selected="True">(None)</asp:ListItem>
            </asp:DropDownList>
            


注意:为DropDownList 添加ListItems可以通过设计器或者声明语法来完成。当添加一个表示数据库空值的item时,要确保是通过声明语法来完成的。如果你使用设计器的ListItem集合编辑器,当赋空字符串时,产生的声明语法会忽略Value的设置,产生想<asp:ListItem>(None)</asp:ListItem>这样的语句。这个看起来并没有什么关系,但是缺少Value会让DropDownList 使用Text属性的值作为Value。这意味着当NULL  ListItem被选择时,“(None)” 会被赋给product的CategoryID或SupplierID字段,这会引起异常。而显式的将Value设为“”,当NULL  ListItem被选择时一个空值会被赋给product的CategoryID或SupplierID字段。

现在浏览该页。当编辑product时,注意Categories和Suppliers DropDownLists 开头都包含一个“(None)”选项。

图 10: e Categories 和Suppliers DropDownLists包含 “(None)”


为了将“(None)” 保存为数据库NULL值,我们需要回到UpdateCommand事件处理。将categoryIDValue和supplierIDValue变量设为可为空的整型,并在DropDownList的SelectedValue的值不为空字符串时才为它们赋值。

C#
1
            2
            3
            4
            5
            6
            7
            
int? categoryIDValue = null;
            if (!string.IsNullOrEmpty(categories.SelectedValue))
            categoryIDValue = Convert.ToInt32(categories.SelectedValue);
            int? supplierIDValue = null;
            if (!string.IsNullOrEmpty(suppliers.SelectedValue))
            supplierIDValue = Convert.ToInt32(suppliers.SelectedValue);
            


完成这个后,如果用户在DropDownList里选择“(None)” 时,一个空值将被传给UpdateProduct BLL方法,它相当于数据库的NULL值。

总结


本章我们学习了如何为DataList创建一个更复杂的编辑界面,它包含三种不同的web控件— 一个TextBox,两个DropDownLists和一个CheckBox —并且包含验证控件。当创建编辑界面时,不管使用什么控件,步骤都是一样的:先为EditItemTemplate添加控件;将字段值通过绑定语法赋给对应的web控件的属性;在UpdateCommand事件处理中编程访问web控件和它们的属性,并将值传给BLL。


当创建编辑界面时,无论它仅仅是由TextBox组成还是由不同的web控件组成,要确保正确的处理数据库NULL值。当处理NULL时,不仅需要在编辑界面正确显示已经存在的NULL值,还需要提供输入NULL值的方法。对DataLists里的DropDownLists 来说,这通常意味着需要添加一个Value属性被显式设为空字符串(Value="")的ListItem,然后在UpdateCommand事件处理里判断NULL ListItem是否被选中。

祝编程愉快!

 
手动以及使用torch.nn实现前馈神经网络实验 手动以及使用torch.nn实现前馈神经网络 手动实现前馈神经网络解决上述回归、二分类、多分类任务 利用torch.nn解决上述回归、二分类、多分类任务 在多分类任务中使用至少三种不同的激活函数 对多分类任务中的模型评估隐藏层层数和隐藏单元个数对实验结果的影响 阅读详情

相关推荐

【笔记】2022.6.7 数据分析概论

已完结

SpriteNym的博客 1378

选择类型控件[控件虽然好学,用起来还是不是那么容易的~!]

选择类型控件 图形类型控件 属性:CssClass、ReadOnly、 1、panel控件用来盛放一些控件。如果设定GroupingText属性那么就渲染为含有的 div标签,也就是GroupBox效果,否则渲染为 shift+F6 生成 选择控件,又称数据绑定控件 1.ListBox 类表框,控件提供的是单选或多重选择列表。通过修改Selectionmode属性来实现C hec

zhanglan478950的专栏 2582

R语言基础之R语言入门

R语言最初是由新西兰奥克兰大学统计系的教授Ross Ihaka和Robert Gentleman在S语言基础上开发完成的。是一门解释性语言。在我看来R语言是一门数学性极强的语言,或者说这是一门为数学而生的语言,因为其具有极其出色的计算与统计分析能力...

weixin_46912400的博客 5万+

ASP.NET 2.0:如何让DropDownList同时拥有数据来源项目与自订项目

图表1   其实在许多时候,我们会希望网页上的DropDownList控件同时拥有数据来源项目与自订项目。这样说如果您还不是很明白,请看图表1所示的网页范例。我们发现,第一个选项「请选择一个部门」显然是一个自订项目,而其它选项则是来自数据表的「部门」字段。以本范例而言,欲完成这样的设计,请依序进行下列设定:   1.           首先,请于网页上选取您的DropDownList...

weixin_33858336的博客 158

Python实战:超分辨率图像质量评估全攻略——PSNR、SSIM、LPIPS与NIQE的代码实现与优化

本文详细介绍了在Python中实现超分辨率图像质量评估的完整方案,涵盖PSNR、SSIM、LPIPS和NIQE四大核心指标。通过模块化代码设计、多进程并行处理和GPU加速优化,帮助开发者构建高效、可扩展的评估系统,并提供了实战避坑指南与结果解读方法。

weixin_29053073的博客 486

故障处理:Oracle一体机磁盘故障时磁盘组重平衡失败的故障处理

摘要: Oracle Exadata一体机出现磁盘故障案例,存储节点故障灯未亮但磁盘已产生大量坏块,导致重平衡失败。文章强调了一体机硬件巡检需检查6个关键维度:节点messages日志、存储cell告警日志、CellCLI日志、ASM告警日志、ILOM日志及服务器信号灯。故障分析显示硬盘物理故障导致磁盘组冗余受损,自动修复流程受阻。文中详细记录了通过CellCLI工具排查损坏磁盘的过程,并提供了完整的故障处理流程和命令输出。文章同步发布于微信公众号"IT民工的龙马人生"和博客网站(www

提供数据库技术支持(系统优化,故障处理,安装升级,数据恢复等) 1240

ASP.NET的控件变量类型:ListItem、DropDownList

1.面向对象思想: 利用控件变量类型:ListItem创建一个变量对象、方法 ListItem obj_li = new ListItem(); protected void Button3_Click(object sender,EventArgs e) { ListItem obj_li = new ListItem(); obj_li.Text = "谷歌"

浓汤拉面的专栏 1656

ICC2: pin shape不在track上引起的drc如何解决?

我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧?

m0_61544122的博客 450

Scott Mitchell 的ASP.NET 2.0数据教程之四十一::实现开放式并发

Scott Mitchell 的ASP.NET 2.0数据教程之四十一::实现开放式并发 导言对于可能存在多用户同时更新或删除数据的web程序来说,存在一个用户的修改覆盖另一个用户的情况。当设计这样的程序时,选择适当的并发控制技术非常重要。我们在实现开放式并发 里已经讨论过,有三种concurrency control (并发控制)的策略:什么都不做—如果并发用户修改的是同一条记录,让

heker2007的专栏 2051

《QGIS快速入门与应用基础》230:添加图例工具

本文系统讲解QGIS图例制作的核心方法与优化技巧。首先强调图例与地图框的绑定逻辑,提出三种绑定状态及适用场景。详细拆解三种图例添加方法:布局工具栏一键添加法(新手首选)、菜单栏添加法和右键快捷创建法,并给出单地图框和多地图框两种典型场景的规范技巧。重点解决新手常见的图例杂乱、样式不规范、内容脱节等问题,帮助用户从基础图例制作进阶到符合行业标准的专业图例设计。通过分步操作图示和实用技巧,使读者能够快速掌握图例创建的核心流程与优化方法。

LuckyHanMo的博客 563

【华为OD】华为性格测试(综合测试)高分策略

1、性格测试非常重要,是一次否决制,18 个月有效期,所以态度一定要认真对待,不要随便乱选;2、做题原则:正向原则,积极乐观向上3、时间为 25-30 分钟,会频繁出现重复选项重复出现的题目注意一致性。

笑着的程序员的博客 3万+

Scott Mitchell 的ASP.NET 2.0数据教程之三十一::使用DataList来一行显示多条记录

  Scott Mitchell 的ASP.NET 2.0数据教程之三十一::使用DataList来一行显示多条记录 导言在前两章的做的DataList的例子里我们都是使用单列的HTML来显示数据.而自定义使DataList将数据显示在多列多行的table里也非常容易.而且还可以以单行多列来显示数据.我们可以通过RepeatColumns和RepeatDirection属性来自定义Da

heker2007的专栏 1723

Android日志实战——Tombstones日志分析(七)

Tombstones 一般是由 Dalvik 错误、状态监视调试器、C 层代码以及 libc 的一些问题导致的。当系统发生 Tombstone 的时候,内核(kernel)首先会上报一个严重的警告信号(signal),上层接收到之后,进程的调试工具会把进程中当时的调用栈现场保存起来,并在系统创建了 data/tombstones 目录后把异常时的进程信息写在此目录里面,开发者需要通过调用栈来分析整个调用流程来找出出问题的点。Tombstone原因分析pid 与 tid 相同,问题出在主线程。

小旭的专栏 2533

Scott Mitchell 的ASP.NET 2.0数据教程之四十六::DataList和Repeater里的自定义button

原文 | 下载本教程中的编码例子 | 下载本教程的PDF版导言在前面关于DataList 和Repeater 的7章教程里,我们分别创建了只读和可以编辑删除的例子。为了让DataList有编辑和删除的功能,我们在ItemTemplate里添加了一些button,当点击时,引起postback,并根据button的CommandName属性激发相关的事件。例如,添加一个CommandName

2077

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Scott Mitchell 的ASP.NET 2.0数据教程之二十九:: 用DataList和Repeater来显示数据

原文| 下载本教程中的编码例子 | 下载本教程的PDF版 导言 在之前的28篇教程的例子里,如果我们需要显示某个数据源的多条记录,我们使用GridView .GridView 的一行表示数据源的一条记录,列表示一个字段.虽然GridView 用来显示数据,分页,排序,编辑,删除非常的方便,但是有点臃肿.而且GridView 结构的标记是固定的—它包含一个带有&lt...

abc0412的博客 227

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Scott Mitchell 的ASP.NET 2.0数据教程之四十五::DataList和Repeater数据排序(一)

原文| 下载本教程中的编码例子 | 下载本教程的PDF版 (以后比较长的部分我都会分几次贴出来) 导言 DataList和Repeater数据分页里我们学习了如何在DataList里添加分页功能。我们在ProductsBLL类里创建了一个名为GetProductsAsPagedDataSource的方法,它返回一个PagedDataSource对象。当绑定到DataList或...

abc0412的博客 219

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Scott Mitchell 的ASP.NET 2.0数据教程之四十四::DataList和Repeater数据分页

原文| 下载本教程中的编码例子 | 下载本教程的PDF版 导言 分页和排序是显示数据时经常用到的功能。比如,在一个在线书店里搜索关于ASP.NET 的书的时候,可能结果会是成百上千,而每页只列出十条。而且结果可以根据title(书名),price(价格),page count(页数),author name(作者)等来排序。我们在分页和排序报表数据 里已经讨论过, Grid...

abc0412的博客 243

Scott Mitchell 的ASP.NET 2.0数据教程之三十:: 格式化DataList和Repeater的数据

原文| 下载本教程中的编码例子 | 下载本教程的PDF版 导言 在前面的教程里我们学习了DataList提供了一些风格样式的属性.而且我们还学习了如何定义HeadStyle, ItemStyle, AlternatingItemStyle, 和SelectedItemStyle等属性的默认CSS.除了这四个属性外,DataList还提供了其它属性,比如Font, For...

abc0412的博客 221

Scott Mitchell 的ASP.NET 2.0数据教程之三十三:: 使用DropDownList过滤的主/从报表

Scott Mitchell 的ASP.NET 2.0数据教程之三十三:: 使用DropDownList过滤的主/从报表 导言在前面的使用DropDownList过滤的主/从报表一章里我们使用GridView创建的主/从表,显示一些"主"记录.用户可以根据主记录来查看"从"(详细)的内容.主/从表在呈现一对多关系和含多列的表的信息时是一个好的选择.在前面我们已经学过如何使用GridView

heker2007的专栏 1311
上一篇: Scott Mitchell 的ASP.NET 2.0数据教程之三十九:: 在编辑和插入界面里添加验证控件
下一篇: Scott Mitchell 的ASP.NET 2.0数据教程之四十二::为删除数据添加客户端确认
wanghr74
博客等级 码龄23年 1粉丝 4原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值