Datalist选择和编辑数据

订餐系统——Gridview、Repeater和DataList 区别 上篇主要介绍了我们项目中用到的Gridview控件,而与该控件相关的还有两种:DataList 和repeater。之前也用过repeater控件,那么他们的主要区别是什么呢?    GridView:      相对来说它的功能最强大,提供分页,编辑,删除,选择等等很多功能,使用最方便,而且支持分页功能。但是因为模板已经定制好,灵活控制性相对较低。      缺点:占用系统资源大,他的分页 阅读详情

<form runat="server">

  <ASP:DataList id="MyDataList" runat="server"
       CellSpacing = "2"
       SelectedItemStyle-BackColor="red"
       SelectedItemStyle-ForeColor="white"
       EditItemStyle-BackColor="yellow"
       EditItemStyle-ForeColor="black"
       DataKeyField="ISBN"//调用datalist的DataKeys("索引")将返回该索引行的ISBN栏的值
       OnItemCommand="DoItemSelect"//以下四个按钮都会触发该事件,故需要commandname来区别不同按钮
       OnEditCommand="DoItemEdit"
       OnUpdateCommand="DoItemUpdate"
       OnDeleteCommand="DoItemDelete"
       OnCancelCommand="DoItemCancel">

    <HeaderTemplate>
      <b>Some Wrox Press Books:</b><br />
    </HeaderTemplate>

    <ItemTemplate>
      //定义CommandName="Select",点击后将会使点击行呈现为SelectedItemTemplate定义内容
      <ASP:Button CommandName="Select" Text="Info" runat="server" />
      <%# DataBinder.Eval(Container.DataItem, "Title") %>
    </ItemTemplate>

    <SelectedItemTemplate>
      Title: <b><%# DataBinder.Eval(Container.DataItem, "Title") %></b><br />
     //定义CommandName="Edit" ,点击后将会使点击行呈现为EditItemTemplate定义内容
     //以及触发EditCommand事件(以及ItemCommand事件)
      <ASP:Button CommandName="Edit" Text="Edit" runat="server" />
      ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %> &nbsp;
      Published:
      <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
    </SelectedItemTemplate>

    <EditItemTemplate>
      <b>ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %></b> &nbsp;
      //定义了三个button的CommandName不同属性
       //CommandName="Update" 点击触发UpdateCommand事件(以及ItemCommand事件)
      <ASP:Button CommandName="Update" Text="Update" runat="server" />
       //CommandName="Delete"点击触发DeleteCommand事件(以及ItemCommand事件)
      <ASP:Button CommandName="Delete" Text="Delete" runat="server" />
     //CommandName="Cancel"点击触发CancelCommand事件(以及ItemCommand事件)
      <ASP:Button CommandName="Cancel" Text="Cancel" runat="server" /><br />
      Title:
      <ASP:TextBox id="txtTitle" Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
                   size="46" runat="server" /><br />
      PublicationDate:
      <ASP:TextBox id="txtPubDate" size="20" runat="server"
                   Text='<%# DataBinder.Eval(Container.DataItem, "PublicationDate") %>'/>
    </EditItemTemplate>

  </ASP:DataList>

</form>
脚本:
<script language="C#" runat="server">

 void Page_Load(Object sender, EventArgs e)
 {
  if (!Page.IsPostBack)
   BindDataGrid();  // create data set and bind to list control
 }

       //此事件函数四个按钮都会触发
 void DoItemSelect(Object objSource, DataListCommandEventArgs objArgs )
 {
  lblSQL.Text = "";  // clear any content from SQL statement Label

  // see if it was the Select button that was clicked
                 //判断是否为Info按钮(通过比较commandname属性)
  if (objArgs.CommandName == "Select")
  {
   // set the SelectedIndex property of the list to this item// s index
   MyDataList.SelectedIndex = objArgs.Item.ItemIndex;
   BindDataGrid();  // bind the data and display it
  }
 }


 void DoItemEdit(Object objSource, DataListCommandEventArgs objArgs)
 {
  // set the SelectedIndex property of the list to -1 to "unselect" it
  MyDataList.SelectedIndex = -1;

  // set the EditItemIndex property of the list to this item's index
  MyDataList.EditItemIndex = objArgs.Item.ItemIndex;
  BindDataGrid();  // bind the data and display it
 }


 void DoItemUpdate(Object objSource, DataListCommandEventArgs objArgs)
 {
  // get a reference to the title and publication date text boxes
  TextBox objTitleCtrl = (TextBox)objArgs.Item.FindControl("txtTitle");
  TextBox objPubDateCtrl = (TextBox)objArgs.Item.FindControl("txtPubDate");
  
  // create a suitable SQL statement and execute it
  string strSQL = "UPDATE Booklist SET Title='" + objTitleCtrl.Text + "', "
       + "PublicationDate='" + objPubDateCtrl.Text + "' "
       + "WHERE ISBN='" + MyDataList.DataKeys[objArgs.Item.ItemIndex] + "'";
  ExecuteSQLStatement(strSQL);

  // set EditItemIndex property of grid to -1 to switch out of Edit mode
  MyDataList.EditItemIndex = -1;
  BindDataGrid();  // bind the data and display it
 }
 

 void DoItemDelete(Object objSource, DataListCommandEventArgs objArgs)
 {
  // create a suitable SQL statement and execute it
  string strSQL = "DELETE FROM Booklist WHERE ISBN='"
      + MyDataList.DataKeys[objArgs.Item.ItemIndex] + "'";
  ExecuteSQLStatement(strSQL);

  // set EditItemIndex property of grid to -1 to switch out of Edit mode
  MyDataList.EditItemIndex = -1;
  BindDataGrid();  // bind the data and display it
 }


 void DoItemCancel(Object objSource, DataListCommandEventArgs objArgs)
 {
  // set EditItemIndex property of grid to -1 to switch out of Edit mode
  MyDataList.EditItemIndex = -1;
  BindDataGrid();  // bind the data and display it
 }


 void ExecuteSQLStatement(string strSQL)
 {
  // this is where the SQL statement would be executed against the
  // original data source. In this example, we're simply displaying
  // the statement in a Label on the page
  lblSQL.Text = "<b>The SQL statement that would be executed is:</b><br />" + strSQL;
 }


 void BindDataGrid()
 {
  // get connection string from ../global/connect-strings.ascx user control
  string strConnect = ctlConnectStrings.OLEDBConnectionString;
               
           
  // create a SQL statement to select some rows from the database
  string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%18610025%'";

  // create a variable to hold an instance of a DataReader object
  OleDbDataReader objDataReader;

  try
  {
   // create a new Connection object using the connection string
   OleDbConnection objConnect = new OleDbConnection(strConnect);

   // open the connection to the database
   objConnect.Open();

   // create a new Command using the connection object and select statement
   OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect);

   // execute the SQL statement against the command to get the DataReader
   objDataReader = objCommand.ExecuteReader();
  }
  catch (Exception objError)
  {
   // display error details
   outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
        + objError.Message + "<br />" + objError.Source + "<p />";
   return;  //  and stop execution
  }

  // set the DataSource property and bind the list
  MyDataList.DataSource = objDataReader;
  MyDataList.DataBind();
 }

</script>

小结:默认下通过设置asp:button的commandname属性为Edit,Update,Delete,Cancel来激发EditCommand,UpdateCommand,
DeleteCommand和CancelCommand事件。
当然这些button也会触发ItemCommand事件。
注:若要在初始时直接进入编辑模式去掉选中状态,可以这样:
 <ItemTemplate>
      //定义CommandName="Edit",点击后将会使点击行呈现为EditItemTemplate定义内容
      <ASP:Button CommandName="Edit" Text="Edit" runat="server" />
      <%# DataBinder.Eval(Container.DataItem, "Title") %>
    </ItemTemplate>
除去 <SelectedItemTemplate>的定义和ItemCommand事件和处理函数的定义

datalist简单用法 简述html中datalist的一些简单应用场景以及示例。 立即下载

相关推荐

在DataList里编辑和删除数据

在DataList里编辑和删除数据 在DataList里编辑和删除数据

谈谈自定义报表和可视化监控大屏设计

今天谈下自定义报表和可视化监控大屏设计方面的内容,准备分三个部分来讲,第一是先看下当前主流的商用报表工具的功能特点,其次分别谈下报表设计和可视化监控大屏设计。 商用报表工具的实现思路 图片来源网络 最近在网上搜索自定义报表,监控大屏制作,搜索到了FineReport报表制作工具。 FineReport报表软件是一款纯功能于一身的企业级web报表工具,它“专业、简捷、灵活”的特点和无码理念,仅需简单的拖拽操作便可以设计复杂的中国式报表,搭建数据决策分析系统。 这款报表制作工具还是相对的火,你能够

weberhuangxingbo的博客 2712

保姆级教程:在RK3568开发板上手把手配置RMII百兆以太网DTS节点

本文提供了一份详细的RK3568开发板RMII百兆以太网DTS节点配置教程,涵盖基础概念解析、关键参数设置、实际操作步骤及常见问题排查。通过手把手指导,帮助开发者快速完成RK3568开发板的以太网配置,特别适合嵌入式系统开发者参考。

weixin_33686714的博客 137

【DataRoom】- 基于VUE的开源的大屏可视化设计器

DataRoom是一款基于SpringBoot、MyBatisPlus、Vue、ElementUI、G2Plot、Echarts等技术栈的大屏设计器,具备大屏设计、预览能力,支持MySQL、Oracle、PostgreSQL、SQLServer、ElasticSearch、JSON、JS、HTTP、Groovy等数据集接入,使用简单,完全免费,代码开源。

老杜的博客 1万+

Nuclei漏洞扫描:从零到精通的Windows实战指南

本文详细介绍了Nuclei漏洞扫描工具在Windows平台上的安装与使用教程,从基础安装到高级实战技巧全面覆盖。通过具体案例展示如何利用Nuclei的模板库快速检测常见漏洞,并提供自定义POC开发指南,帮助安全从业者高效开展漏洞扫描工作。

weixin_28683689的博客 249

配置oem 13c以监控管理数据库

使用oem13c监控数据库

雅冰石的专栏 1808

DataList控件详细用法(一)

使用DataList控件  本章内容:         1、理解事件冒泡         2、使用模板         3、在DataList中显示数据         4、在DataList中创建多列         5、捕获DataList控件中产生的事件         6、选择DataList中的项         7、使用DataList控件中的DataKey

平凡酿就非凡 3万+

CentOS7下NFS共享报错RPC问题的3种解决方案(含防火墙配置详解)

本文详细解析了CentOS7下NFS共享报错RPC问题的3种解决方案,包括防火墙配置详解。从基础连通性检查到RPC服务状态验证,再到精准开放必要端口和SELinux集成,提供全面的排查与解决指南,帮助用户快速解决'No route to host'等常见问题。

weixin_29274969的博客 452

DATALIST 控件学习及常见操作总结(显示,索引,分页,编辑操作)

 MSDN:DataList Web 服务器控件以某种格式显示数据,这种格式可以使用模板和样式进行定义。DataList 控件可用于任何重复结构中的数据,如表。DataList 控件可以以不同的布局显示行,如按列或行对数据进行排序。也就是说一般情况我们用的GIRDVIEW控件只显示一行一条的话,那么DataList 可以在一行上显示多条数据。一 绑定数据源 并显示数据1.WEB.CON

jiayue's BLOG 6177

从放弃到再入门之拉格朗日对偶问题推导

普通同学的解法 无约束条件:求导就可以了 等式约束:代入消元,再求导 不等式约束:分情况讨论(在边界上和不在边界上),分别对应1,2的情况 然而发现,有些情况消元特别复杂,甚至不能求解 聪明同学的解法 发现:在最优点的情况下,约束曲面的法向量和目标函数的梯度反向必相同或相反 拉格朗日乘子法如何理解? (在2维里可以形象的解释为z=f(x,y)在约束条件g(x,y)=c中,极值...

qq_34564612的博客 1万+

一文就够-InfiniBand 技术全面解析:从原理到实践的通俗易懂指南

想象一下,在一个超级数据中心里,成千上万台服务器需要像蜜蜂群体一样高效协作。如果用传统的网络连接,就像让每只蜜蜂都排着队传递消息,效率极低。InfiniBand(简称 IB)就是为了解决这种 "通信拥堵" 而生的高速网络技术,它专门为高性能计算(HPC)、数据中心和云计算环境设计,就像给服务器之间搭建了多条高速公路,让数据可以飞驰而过。

jundao1997的专栏 1394

开源大屏设计工具DataRoom

定位:DataRoom旨在提供一个安全、高效的数据共享和协作平台,同时也作为大屏可视化设计器使用。技术栈:DataRoom基于SpringBoot、MyBatisPlus、Vue、ElementUI、G2Plot、Echarts等技术栈开发。特点:完全免费、代码开源,支持多种数据源和数据集接入,提供丰富的图表组件和设计工具。DataRoom作为一个开源项目,不仅提供了强大的数据共享与协作功能,还具备大屏可视化设计器的特性,为团队提供了高效、安全的数据处理和可视化解决方案。

xuweilin的博客 6910

YOLOv11 模型改进全流程:从代码修改到性能跃升,让你打造专属检测利器

无论是科研中需要发表创新点满满的论文,还是企业里要解决特定场景的检测难题,这套方法都能让你在YOLOv11的基础上打造出“量身定制”的检测模型。别再让通用模型的“平庸性能”限制你的项目上限——现在就动手修改代码,让你的YOLOv11模型在精度、速度或小样本适应能力上实现跨越式提升,用技术突破证明你的AI研发实力!本文将带你从代码层面入手,完成从骨干网络到损失函数的全维度改进,真正掌握让YOLOv11“为你所用”的技术实力。该改进对小样本类别(如数据集中仅占5%的“稀有缺陷”)的识别精度提升显著。

m0_71589036的博客 402

DataList数据绑定

csharpyisi的专栏 897

【报错解决】OpenClaw 报错 ENOENT: no such file or directory, open ‘/root/.npmrc‘:配置文件路径不存在

本文分析了OpenClaw平台在国产操作系统环境下运行时出现的ENOENT: no such file or directory, open '/root/.npmrc'错误。该问题源于容器环境中root用户的npm配置文件缺失,导致插件依赖安装失败。文章详细解释了npm配置加载机制和容器环境的特殊性,提出了四种解决方案:手动创建配置文件、写入基础配置、使用全局配置替代以及容器启动时自动初始化。最后指出该错误暴露了工程中对默认路径盲目信任的问题,强调显式声明运行环境依赖的重要性。

博客 2015

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

ASP.NET DataList控件的了解与使用

ASP.NET DataList控件的了解与使用DataList控件概述使用DataList绑定数据源设计DataList控件模板 DataList控件概述 看了些帖子,有说到和Repeater控件类似,博主刚接触ASP.NET没多久,先学了DataList,还没接触过Repeater控件,就记录下所学所得。 DataList控件可以使用模板及定义样式来显示数据,并且还能对数据进行选择、删除及编辑...

victorzhangwt的博客 3205

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

《ASP.NET》数据绑定—DataList

DataList控件是.NET中的一个控件。DataList控件以表的形式呈现数据(在属性生成器中能够编辑),通过该控件,您能够使用不同的布局来显示数据记录(使用模板编辑)。比如,将数据记录排成列或行的形式。您能够对 DataList控件进行配置,使用户可以编辑或删除表中的记录(使用EditItemTemplate模板和SelectedItemTemplate模板)...

weixin_30402085的博客 191

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

DataList教程

DataList<spanstyle='font-size:14.0pt;mso-bidi-font-size:10.0pt;font-family:宋体;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>控件的使用<spanlang=EN-US style='fo...

weixin_30873847的博客 249

数据绑定控件之一DataList控件的使用

本章内容: 1、理解事件冒泡 2、使用模板 3、在DataList中显示数据 4、在 DataList中创建多列 5、捕获DataList控件中产生的事件 6、选择DataList中的项 7、使用DataList控件中的DataKeys集

1087

DataList控件的使用

DataList控件的使用<br />使用DataList控件<br /><br />本章内容:<br /><br />Ø 1、理解事件冒泡<br /><br />Ø 2、使用模板<br /><br />Ø 3、在DataList中显示数据<br /><br />Ø 4、在DataList中创建多列<br /><br />Ø 5、捕获DataList控件中产生的事件<br /><br />Ø 6、选择DataList中的项<br /><br />Ø 7、使用DataList控件中的DataKeys集合<br

游戏全栈,技术合作 1688

DataGrid, DataList and Repeater

Quick and handy guide in deciding which data controls to use :DataGrid - Great flexibility, provides data paging, editing and sorting features. - Easy and quick to develop.- Massive features and ease ...

weixin_33851177的博客 98

数据列表DataList模板之实例

1,数据列表DataList与重复列表Repeator很类似,但是DataList应用更广泛,因为他可以选择和修改数据项的内容。 DataList的数据显示和布局与Repeator控件一样都是通过“模板”控制的。 (注:模板至少要定义一个“数据项模板”(ItemTemplate)来指定显示布局)   2,DataList支持的模板类型: 模板 名称

葵歌小妖 6636

DataList用法总结

设计模版: 页眉<HeaderTemplate> </HeaderTemplate> 页脚<FooterTemplate> </FooterTemplate> 数据记录<ItemTemplate> </ItemTemplate> <AlternatingItemTemplate&g...

weixin_30741653的博客 127

DataList-----------1

使用DataList控件  本章内容:          1、理解事件冒泡          2、使用模板          3、在DataList中显示数据          4、在DataList中创建多列          5、捕获DataList控件中产生的事件          6、选择DataList中的项         

WUSHAOHONGSHIHUO的专栏 395
上一篇: 利用XSLT把ADO记录集转换成XML
下一篇: 利用datagrid编辑数据
AllenTing
博客等级 码龄22年 2粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值