DataList控件可以使用模板与定义样式来显示数据并进行数据的选择, 删除及编辑工作. DataList控件的最大特点是一定要通过模板来定义数据的显示格式. 如果要设计出美观的界面, 就需要花费一番心思. DataList控件显示数据时具有灵活性, 开发人员发挥的空间较大, DataList支持的模板如下:

AlternationItemTemplate

如果已经定义, 则为DataList中的交替项提供内容和布局; 如果未定义, 则使用ItemTemplate

EditItemTemplate

如果已经定义, 则为DataList中的当前编辑项提供内容和布局; 如果未定义则使用ItemTemplate

FooterTemplate

如果已经定义, 则为DataList的脚注部分提供内容和布局; 如果未定义则不显示脚注部分

HeaderTemplate

如果已经定义, 则为DataList的页眉部分提供内容和布局; 如果未定义则不显示页眉部分

ItemTemplate

为DataList中的项提供内容和布局所要求的模板

SelectdItemTemplate

如果已经定义, 则为DataList中的当前选定项提供内容和布局; 如果未定义则使用ItemTemplate

SeparatorTemplate

如果已经定义, 则为DataList中的分隔符提供内容和布局; 如果未定义则不显示分隔符

下面是关于dataList控件的前端代码简单示例:

<asp:DataList ID="DataList1" runat="server" Width="239px">
<FooterTemplate>
<table border="1" style="width: 300px; text-align: center;" cellpadding="0" cellspacing="0">
<tr>
<td colspan="4" style="font-size: 16pt; color: #006600; text-align: center">
下面这行是合计</td>
</tr>
<tr>
<td style="height: 19px; width: 50px; color: #669900;">
编号合计</td>
<td style="height: 19px; width: 50px; color: #669900;">
姓名合计</td>
<td style="height: 19px; width: 50px; color: #669900;">
性别合计</td>
<td style="width: 150px; height: 19px; color: #669900;">
内号码合计</td>
</tr>
</table>
</FooterTemplate>
<HeaderTemplate>
<table border="1" style="width: 300px; text-align: center;" cellpadding="0" cellspacing="0">
<tr>
<td colspan="4" style="font-size: 16pt; color: #006600; text-align: center">
使用DataList控件绑定数据源</td>
</tr>
<tr>
<td style="height: 19px; width: 50px; color: #669900;">
编号</td>
<td style="height: 19px; width: 50px; color: #669900;">
姓名</td>
<td style="height: 19px; width: 50px; color: #669900;">
性别</td>
<td style="width: 150px; height: 19px; color: #669900;">
内号码</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table border="1" style="width: 300px; color: #000000; text-align: center;" cellpadding="0" cellspacing="0">
<tr>
<td style="height: 21px; width: 50px; color: #669900;">
<asp:Label ID="lblStuID" runat="server" Text='<%# Eval("cardNo") %>'></asp:Label></td>
<td style="height: 21px; width: 50px; color: #669900;">
<asp:Label ID="lblStuName" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
<td style="height: 21px; width: 50px; color: #669900;">
<asp:Label ID="lblStuSex" runat="server" Text='<%# Eval("sex") %>'></asp:Label></td>
<td style="width: 150px; height: 21px; color: #669900;">
<asp:Label ID="lblstuHobby" runat="server" Text='<%# Eval("cardBound") %>'></asp:Label></td> </tr>
</table>
</ItemTemplate>
</asp:DataList>

对应的后台代码:

 protected void Page_Load(object sender, EventArgs e)

     {

         if (!IsPostBack)

         {

             //实例化SqlConnection对象

             SqlConnection sqlCon = new SqlConnection();

             //实例化SqlConnection对象连接数据库的字符串

             sqlCon.ConnectionString = "server=.;uid=sa;pwd=123.456;database=TYW";

             //定义SQL语句

             string SqlStr = "select * from card";

             //实例化SqlDataAdapter对象

             SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);

             //实例化数据集DataSet

             DataSet ds = new DataSet();

             da.Fill(ds, "card");

             //绑定DataList控件

             DataList1.DataSource = ds;//设置数据源,用于填充控件中的项的值列表

             DataList1.DataBind();//将控件及其所有子控件绑定到指定的数据源

         }

     }

对应的显示效果图:

DataList一些基本的事件使用简单示例:

 public SqlConnection GetCon()

     {

         //实例化SqlConnection对象

         SqlConnection sqlCon = new SqlConnection();

         //实例化SqlConnection对象连接数据库的字符串

         sqlCon.ConnectionString = "server=.;uid=sa;pwd=123.456;database=TYW";

         return sqlCon;

     }

     public void Bind()

     {

         SqlConnection sqlCon = GetCon();

         //定义SQL语句

         string SqlStr = "select * from card";

         //实例化SqlDataAdapter对象

         SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);

         //实例化数据集DataSet

         DataSet ds = new DataSet();

         da.Fill(ds, "card");

         //绑定DataList控件

         DataList1.DataSource = ds;//设置数据源,用于填充控件中的项的值列表

         DataList1.DataKeyField = "cardNo";//设置数据表的主键

         DataList1.DataBind();//将控件及其所有子控件绑定到指定的数据源

     }

     protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)

     {

         //设置DataList1控件的编辑项的索引为选择的当前索引

         DataList1.EditItemIndex = e.Item.ItemIndex;

         //数据绑定

         Bind();

     }

     protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)

     {

         //设置DataList1控件的编辑项的索引为-1,即取消编辑

         DataList1.EditItemIndex = -;

         //数据绑定

         Bind();

     }

     protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)

     {

         //取得编辑行的关键字段的值

         string stuID = DataList1.DataKeys[e.Item.ItemIndex].ToString();

         //取得文本框中输入的内容

         string stuName = ((TextBox)e.Item.FindControl("txtName")).Text;

         string stuSex = ((TextBox)e.Item.FindControl("txtSex")).Text;

         string stuHobby = ((TextBox)e.Item.FindControl("txtHobby")).Text;

         string sqlStr = "update card set name='" + stuName + "',sex='" + stuSex + "',cardBound='" + stuHobby + "' where cardNo=" + stuID;

         //更新数据库

         SqlConnection myConn = GetCon();

         myConn.Open();

         SqlCommand myCmd = new SqlCommand(sqlStr, myConn);

         myCmd.ExecuteNonQuery();

         myCmd.Dispose();

         myConn.Close();

         //取消编辑状态

         DataList1.EditItemIndex = -;

         Bind();

     }

028. asp.net数据绑定控件值DataList控件的更多相关文章

  1. 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定

    aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...

  2. 027. asp.net中数据绑定控件之 GridView控件

    GridView控件支持下面的功能: 绑定至数据源控件, 如SqlDataSource 内置排序功能 内置更新和删除功能 内置分页功能 内置行选择功能 可以编程方式访问GridView对象模型以动态设 ...

  3. asp.net学习之DataList控件

    asp.net学习之DataList控件   DataList控件与Repeater控件一样由模板驱动,与Repeater控件不同的是: DataList控件默认输出是一个HTML表格.DataLis ...

  4. ASP.NET数据绑定控件简介

    •数据绑定分为数据源和数据绑定控件两部分(①数据绑定控件通过数据源获取和修改数据②数据绑定控件通过数据源隔离数据提供者和数据使用者)数据绑定控件→数据源→数据库•数据源:SqlDataSource(连 ...

  5. asp.net学习之 数据绑定控件--表格绑定控件

    原文:asp.net学习之 数据绑定控件--表格绑定控件     数据绑定 Web 服务器控件是指可绑定到数据源控件,以实现在 Web 应用程序中轻松显示和修改数据的控件.数据绑定 Web 服务器控件 ...

  6. asp.net取HTML控件值

    asp.net取HTML控件值所有html表单里面的值控件,提交后都是以键值 key=value&key=value&……这样的形式提交给后台. radio也一样,会将选中的radio ...

  7. WPFS数据绑定(要是后台类对象的属性值发生改变,通知在“client界面与之绑定的控件值”也发生改变须要实现INotitypropertyChanged接口)

    WPFS数据绑定(要是后台类对象的属性值发生改变,通知在"client界面与之绑定的控件值"也发生改变须要实现INotitypropertyChanged接口) MainWindo ...

  8. ASP.NET MVC加载用户控件后并获取其内控件值或赋值

    有网友看了这篇<ASP.NET MVC加载ASCX之后,并为之赋值>http://www.cnblogs.com/insus/p/3643254.html 之后,问及Insus.NET,不 ...

  9. C# 将Dictionary,StringDictionary等集合数据绑定到如comboBox等控件数据源中将获取健值

    一般在使用C#提供的如combobx控件绑定数据源时都是直接绑定数据库里的数据的(DataTable,DataSet等) 最近在一个项目里需要使用combobox绑定类似“状态的”数据源,该字段里的数 ...

随机推荐

  1. 面向连接的Socket Server的简单实现(简明易懂)

    一.基本原理 有时候我们需要实现一个公共的模块,需要对多个其他的模块提供服务,最常用的方式就是实现一个Socket Server,接受客户的请求,并返回给客户结果. 这经常涉及到如果管理多个连接及如何 ...

  2. iOS interface guidelines (界面设计指南)<一>

    一.      为iOS而设计 1.iOS体现的主题: (1)Deference(顺从):UI的存在就是为了让顾客更加容易理解和进行交互,而不是要和顾客玩智力游戏 (2)Clarity(清晰):在每个 ...

  3. Boostrap栅格系统

    Boostrap排版.链接样式设置了基本的全局样式.分别是:为body元素设置 布局容器:Bootstrap需要为页面内容和栅格系统包裹一个:container容器.Bootstrap提供了两个作此用 ...

  4. Euro Efficiency_完全背包

    Description On January 1st 2002, The Netherlands, and several other European countries abandoned the ...

  5. To and Fro

    Description Mo and Larry have devised a way of encrypting messages. They first decide secretly on th ...

  6. Smart210学习记录------块设备

    转自:http://bbs.chinaunix.net/thread-2017377-1-1.html 本章的目的用尽可能最简单的方法写出一个能用的块设备驱动.所谓的能用,是指我们可以对这个驱动生成的 ...

  7. 触控(Touch) 、 布局(Layout)

    1 使用触控实现一个简易的画板 1.1 问题 触控(Touch)是一个UITouch类型的对象,当用户触摸了屏幕上的视图时自动被创建,通常使用触控实现绘图.涂鸦.手写等功能.本案例使用触控实现一个简易 ...

  8. dedecms 列表每隔6行输出一个空li

    {dede:list pagesize='33'} <li class="hang"><a href="[field:arcurl/]" ta ...

  9. 兼容性所有浏览器的透明CSS设置

    兼容所有浏览器的透明CSS设置: .transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0. ...

  10. js千分位的函数

    不错的js千分位函数,适用于将金额每3位用“,”分隔 /*for price using thousands separator */ function fprice(s,n){ s = parseF ...