• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >ASP.NET > 在ASP.NET 2.0中操作数据之四十:自定义DataList编辑界面

在ASP.NET 2.0中操作数据之四十:自定义DataList编辑界面

作者:heker2007 字体:[增加 减小] 来源:互联网 时间:2017-05-11

heker2007通过本文主要向大家介绍了asp.net,asp net培训,asp和asp.net的区别,零基础学asp.net,c#和asp.net的区别等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

导言

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

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

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

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

http://files.weikejianghu.com/file_images/article/201605/2016051211162634.png
图 1: 编辑界面包含一个TextBox, 两个 DropDownLists和一个CheckBox

第一步: 显示Product 信息

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

http://files.weikejianghu.com/file_images/article/201605/2016051211162635.png
图 2: 在UPDATE, INSERT, DELETE 标签的下拉列表里选择 (None)

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

<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>
</div>

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

http://files.weikejianghu.com/file_images/article/201605/2016051211162636.png
图 3: 显示product信息

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

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

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

http://files.weikejianghu.com/file_images/article/201605/2016051211162637.png
图 4: 为Categories添加一个DropDownList

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

http://files.weikejianghu.com/file_images/article/201605/2016051211162738.png
图 5: 创建 ObjectDataSource

http://files.weikejianghu.com/file_images/article/201605/2016051211162739.png
图 6: 配置DropDownList的 Display 字段和Value 字段

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

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

  最后添加Update 和Cancel button。记得这两个button的CommandName属性必须分别设为“Update” 和“Cancel”。你可以将编辑界面以你喜欢的方式展示。我选择使用和只读界面一样的界面来显示,见下面的声明代码和截图。

<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" DataTextField="CompanyName"
     DataSourceID="SuppliersDataSource"
     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" />
     
    <asp:Button runat="Server" ID="CancelButton" CommandName="Cancel"
     Text="Cancel" CausesValidation="False" />
   </td>
  </tr>
 </table>
 <br />
 <asp:ObjectDataSource ID="CategoriesDataSource" runat="server"
  OldValuesParameterFormatStri



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Asp.net SignalR 应用并实现群聊功能 开源代码
  • asp.net动态更新
  • asp.net利用母版制作页脚效果
  • Asp.Net服务器发送HTTP标头后无法设置内容类型的问题解决
  • 使用asp.net mvc,boostrap及knockout.js开发微信自定义菜单编辑工具(推荐)
  • 详解ASP.NET MVC 常用扩展点:过滤器、模型绑定
  • ASP.NET Core发送邮件的方法
  • 在ASP.NET Core 中发送邮件的实现方法(必看篇)
  • ASP.NET MVC从视图传参到控制器的几种形式
  • Asp.net core WebApi 使用Swagger生成帮助页实例

相关文章

  • 2017-05-11mstest实现类似单元测试nunit中assert.throws功能
  • 2017-05-11获取当前url
  • 2017-05-11ASP.NET开发中经常用到10款工具软件介绍
  • 2017-05-11*.ashx文件不能访问Session值的解决方法
  • 2017-05-11.net非托管资源的回收方法
  • 2017-05-11aspnet_regiis.exe命令使用方法
  • 2017-05-11asp.net获取select值的方法
  • 2017-05-11Asp.Net URL重写的具体实现
  • 2017-05-11ASP.NET MVC数组模型绑定详解
  • 2017-05-11DataList中TextBox onfocus调用后台void静态方法及获取相应行数

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • System.Runtime.InteropServices.COMException的解决方法
    • Asp.net请求处理之管道处理介绍
    • ASP.NET中ImageButton图片按钮控件的使用
    • VS2012实现简单登录界面
    • Asp.net MVC 中利用jquery datatables 实现数据分页显示功能
    • 解析ABP框架中的事务处理和工作单元
    • Asp.Net服务器发送HTTP标头后无法设置内容类型的问题解决
    • Repeater事件OnItemCommand取得行内控件的方法
    • Visual Studio 2017安装失败的解决方法
    • ASP.NET 恢复备份Sqlserver实现代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有