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

AlternationItemTemplate

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

EditItemTemplate

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

FooterTemplate

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

HeaderTemplate

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

ItemTemplate

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

SelectdItemTemplate

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

SeparatorTemplate

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

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

  1. <asp:DataList ID="DataList1" runat="server" Width="239px">
  2. <FooterTemplate>
  3. <table border="1" style="width: 300px; text-align: center;" cellpadding="0" cellspacing="0">
  4. <tr>
  5. <td colspan="4" style="font-size: 16pt; color: #006600; text-align: center">
  6. 下面这行是合计</td>
  7. </tr>
  8. <tr>
  9. <td style="height: 19px; width: 50px; color: #669900;">
  10. 编号合计</td>
  11. <td style="height: 19px; width: 50px; color: #669900;">
  12. 姓名合计</td>
  13. <td style="height: 19px; width: 50px; color: #669900;">
  14. 性别合计</td>
  15. <td style="width: 150px; height: 19px; color: #669900;">
  16. 内号码合计</td>
  17. </tr>
  18. </table>
  19. </FooterTemplate>
  20. <HeaderTemplate>
  21. <table border="1" style="width: 300px; text-align: center;" cellpadding="0" cellspacing="0">
  22. <tr>
  23. <td colspan="4" style="font-size: 16pt; color: #006600; text-align: center">
  24. 使用DataList控件绑定数据源</td>
  25. </tr>
  26. <tr>
  27. <td style="height: 19px; width: 50px; color: #669900;">
  28. 编号</td>
  29. <td style="height: 19px; width: 50px; color: #669900;">
  30. 姓名</td>
  31. <td style="height: 19px; width: 50px; color: #669900;">
  32. 性别</td>
  33. <td style="width: 150px; height: 19px; color: #669900;">
  34. 内号码</td>
  35. </tr>
  36. </table>
  37. </HeaderTemplate>
  38. <ItemTemplate>
  39. <table border="1" style="width: 300px; color: #000000; text-align: center;" cellpadding="0" cellspacing="0">
  40. <tr>
  41. <td style="height: 21px; width: 50px; color: #669900;">
  42. <asp:Label ID="lblStuID" runat="server" Text='<%# Eval("cardNo") %>'></asp:Label></td>
  43. <td style="height: 21px; width: 50px; color: #669900;">
  44. <asp:Label ID="lblStuName" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
  45. <td style="height: 21px; width: 50px; color: #669900;">
  46. <asp:Label ID="lblStuSex" runat="server" Text='<%# Eval("sex") %>'></asp:Label></td>
  47. <td style="width: 150px; height: 21px; color: #669900;">
  48. <asp:Label ID="lblstuHobby" runat="server" Text='<%# Eval("cardBound") %>'></asp:Label></td>
  49.  
  50. </tr>
  51. </table>
  52. </ItemTemplate>
  53. </asp:DataList>

对应的后台代码:

  1. protected void Page_Load(object sender, EventArgs e)
  2.  
  3. {
  4.  
  5. if (!IsPostBack)
  6.  
  7. {
  8.  
  9. //实例化SqlConnection对象
  10.  
  11. SqlConnection sqlCon = new SqlConnection();
  12.  
  13. //实例化SqlConnection对象连接数据库的字符串
  14.  
  15. sqlCon.ConnectionString = "server=.;uid=sa;pwd=123.456;database=TYW";
  16.  
  17. //定义SQL语句
  18.  
  19. string SqlStr = "select * from card";
  20.  
  21. //实例化SqlDataAdapter对象
  22.  
  23. SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
  24.  
  25. //实例化数据集DataSet
  26.  
  27. DataSet ds = new DataSet();
  28.  
  29. da.Fill(ds, "card");
  30.  
  31. //绑定DataList控件
  32.  
  33. DataList1.DataSource = ds;//设置数据源,用于填充控件中的项的值列表
  34.  
  35. DataList1.DataBind();//将控件及其所有子控件绑定到指定的数据源
  36.  
  37. }
  38.  
  39. }

对应的显示效果图:

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

  1. public SqlConnection GetCon()
  2.  
  3. {
  4.  
  5. //实例化SqlConnection对象
  6.  
  7. SqlConnection sqlCon = new SqlConnection();
  8.  
  9. //实例化SqlConnection对象连接数据库的字符串
  10.  
  11. sqlCon.ConnectionString = "server=.;uid=sa;pwd=123.456;database=TYW";
  12.  
  13. return sqlCon;
  14.  
  15. }
  16.  
  17. public void Bind()
  18.  
  19. {
  20.  
  21. SqlConnection sqlCon = GetCon();
  22.  
  23. //定义SQL语句
  24.  
  25. string SqlStr = "select * from card";
  26.  
  27. //实例化SqlDataAdapter对象
  28.  
  29. SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);
  30.  
  31. //实例化数据集DataSet
  32.  
  33. DataSet ds = new DataSet();
  34.  
  35. da.Fill(ds, "card");
  36.  
  37. //绑定DataList控件
  38.  
  39. DataList1.DataSource = ds;//设置数据源,用于填充控件中的项的值列表
  40.  
  41. DataList1.DataKeyField = "cardNo";//设置数据表的主键
  42.  
  43. DataList1.DataBind();//将控件及其所有子控件绑定到指定的数据源
  44.  
  45. }
  46.  
  47. protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
  48.  
  49. {
  50.  
  51. //设置DataList1控件的编辑项的索引为选择的当前索引
  52.  
  53. DataList1.EditItemIndex = e.Item.ItemIndex;
  54.  
  55. //数据绑定
  56.  
  57. Bind();
  58.  
  59. }
  60.  
  61. protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
  62.  
  63. {
  64.  
  65. //设置DataList1控件的编辑项的索引为-1,即取消编辑
  66.  
  67. DataList1.EditItemIndex = -;
  68.  
  69. //数据绑定
  70.  
  71. Bind();
  72.  
  73. }
  74.  
  75. protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
  76.  
  77. {
  78.  
  79. //取得编辑行的关键字段的值
  80.  
  81. string stuID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
  82.  
  83. //取得文本框中输入的内容
  84.  
  85. string stuName = ((TextBox)e.Item.FindControl("txtName")).Text;
  86.  
  87. string stuSex = ((TextBox)e.Item.FindControl("txtSex")).Text;
  88.  
  89. string stuHobby = ((TextBox)e.Item.FindControl("txtHobby")).Text;
  90.  
  91. string sqlStr = "update card set name='" + stuName + "',sex='" + stuSex + "',cardBound='" + stuHobby + "' where cardNo=" + stuID;
  92.  
  93. //更新数据库
  94.  
  95. SqlConnection myConn = GetCon();
  96.  
  97. myConn.Open();
  98.  
  99. SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
  100.  
  101. myCmd.ExecuteNonQuery();
  102.  
  103. myCmd.Dispose();
  104.  
  105. myConn.Close();
  106.  
  107. //取消编辑状态
  108.  
  109. DataList1.EditItemIndex = -;
  110.  
  111. Bind();
  112.  
  113. }

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. 控制HTML的input控件的输入内容

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  2. 如何通过 GT 快速开始性能测试?

    http://gt.tencent.com/docs/a/2.1/GTAndroidQuickStart.pdf Summary 安装 GT(GT.apk)后,不需要连接 PC 和在被测应用中插入代码 ...

  3. Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories.

    修改settings.xml: <mirror> <id>nexus-osc</id> <mirrorOf>*</mirrorOf> < ...

  4. VBS_For_next

    指定循环次数,使用计数器重复运行语句,语法结构如下: 1 2 3 4 5 For counter = start To end [Step step]     [statements]     [Ex ...

  5. 定时同步时间与crontab

    date 月日时分年.秒date -s可以直接设置系统时间 比如将系统时间设定成1996年6月10日的命令如下.#date -s 06/10/96将系统时间设定成下午1点12分0秒的命令如下.#dat ...

  6. cocos2dx 搭建 android 平台 -2

    1.配置Cocos2d-x for Xcode. 这一块比较简单, 可以参见其他文章. 重点:install-templates-xcode.sh 2.配置普通Android开发环境 这一块包括JDK ...

  7. const char* <----- > string

    (1) const char*      <-----     string const char* const_txt_path=txt_path.c_str(); (2)  string  ...

  8. AttributeError: 'dict_values' object has no attribute 'translate'

    /***************************************************************************************** * Attribu ...

  9. dos快速通道

    要在文件夹的右键菜单中添加“命令提示符”选项.在注册表HKEY_CLASSES_ROOT\Directory\shell分支下新建一项“CommandPrompt”,修改右侧窗格中的“默认”键值为“命 ...

  10. 【转帖】ios上取得设备唯一标志的解决方案

    原文地址:http://lqzit.iteye.com/blog/2070306 注意:keychina设置完之后,项目目录里的“项目名.entitlements”文件不是手动创建出的,而是在按照如下 ...