《ASP.NET1200例》在DataList里编辑和删除数据
使用GridView来编辑和删除数据之所以很简单,是因为GridView和ObjectDataSource在底层非常一致。当更新按钮被点击时,GridView自动将字段的值赋给ObjectDataSource的UpdateParameters集合,然后激发ObjectDataSource的Update()方法。而DataList与Repeater并没有相关使用方法。
需要确保将合适的值赋给ObjectDataSource的参数,然后调用Update()方法。DataList提供了以下的属性和事件来完成这些:
DataKeyField property — 更新或删除时,需要唯一确定DataList里的每个item。将这个属性设为显示的数据的主健。这样做会产生DataList的 DataKeys collection ,每个item都有一个指定的 DataKeyField .
EditCommand event — 当CommandName属性设为“Edit”的Button, LinkButton, 或 ImageButton 被点时激发.
CancelCommand event — 当CommandName属性设为“Cancel”的Button, LinkButton, 或ImageButton 被点时激发.
UpdateCommand event — 当CommandName属性设为“Update”的Button,LinkButton, 或ImageButton 被点时激发.
DeleteCommand event — 当CommandName属性设为“Delete”的Button, LinkButton, 或 ImageButton 被点时激发.
使用以上的属性和事件,有四种方法来更新和删除数据:(第一种方法提供了更好的可扩展性,而设计DataList的本意就是使用这种方式。)
1. 使用ASP.NET 1.x 的技术— DataList先于ASP.NET 2.0 和ObjectDataSources 存在,可以直接通过编程来实现编辑和删除。这种方法需要在显示数据或者更新删除记录时,直接在BLL层将数据绑定到DataList。
2. 使用一个单独的ObjectDataSource 来实现选择,更新和删除 — DataList没有GridView内置的编辑删除功能,并不意味着不能添加这些功能。使用 ObjectDataSource,但是在设置ObjectDataSource的参数并调用Update()方法时,需要为DataList的UpdateCommand事件创建一个 event handler。
3. Using an ObjectDataSource Control for Selecting, but Updating and Deleting Directly Against the BLL — 使用第二种方法时需要为UpdateCommand事件和参数赋值等写一些代码。其实我们可以用ObjectDataSource来实现selecting ,更新和删除直接调用BLL(象第一种方法)。直接调用BLL会使代码可读性更好。
4. 使用多个ObjectDataSources —前面的三种方法都需要一些代码。最后一种方法是使用多个ObjectDataSources 。第一个ObjectDataSource 从BLL获取数据,并绑定到 DataList. 为更新添加另一个 ObjectDataSource, 直接添加到DataList的 EditItemTemplate.同样对删除也是如此。三个ObjectDataSource通过ControlParameters声明语法直接将参数绑定到ObjectDataSource 的参数 (而不是在 DataList的 UpdateCommand event handler编程处理). 这种方法也需要一些编码 — 需要调用ObjectDataSource内置的 Update() 或 Delete() — 但是比起其它三种方法,代码少的多。这种方法的劣势是多个ObjectDataSources 使页面看起来混乱。
注意:
1. 当使用ObjectDataSource修改数据时,在声明标记里需要移除OldValuesParameterFormatString (或重新设为缺省值,{0})。在并发控制下可使用original_{0},表示原始数据。
2. DataList有一些属性是编辑和删除需要用到的,这些值都存在view state里。因此创建支持编辑和删除功能的DataList时,DataList的view state需要开启。在创建可编辑的GridView,DetailsViews和FormViews的时候,view state是禁用的。这是因为ASP.NET 2.0 控件包含了control state,它在postback时状态是连续的。在GridView里禁用了view state仅仅只是忽略了无关紧要的状态信息,但是维持了control state(它包含了编辑和删除需要的状态)。而DataList是 ASP.NET 1.x时代创建的,并没有使用control state,因此view state必须开启。
3.默认DataList只有一个ItemTemplate。需要手动添加一个EditItemTemplate来支持编辑功能。
4.DataList并不支持双向绑定,准备更新数据时,需要编程将Textbox的Text的值传给ProductsBLL类的UpdateProduct方法。
当设置了CommandName的Repeater或DataList里的Button,LinkButton或ImageButton被点击时,Repeater或DataList的ItemCommand事件被激发。对DataList来说,如果CommandName设为某个值,另外一个事件也会被激发(除了ItemCommand被激发以外,下面事件也会激发),如下:
“Cancel” — 激发 CancelCommand event
“Edit” — 激发 EditCommand event
“Update” — 激发UpdateCommand event
【进入编辑功能】
点击DataList里的button会引起postback,但是并没有进入product的编辑模式。为了完成这个,需要:
1. 设置DataList的 EditItemIndex property 为 被点击了Edit button的 DataListItem的 index .
2. 重新绑定数据到 DataList. 当 DataList 重新展现时, 和DataList的EditItemIndex相关的DataListItem 会展现EditItemTemplate.
通过以下代码完成:
C#
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
// Set the DataList's EditItemIndex property to the index of the DataListItem that was clicked
DataList1.EditItemIndex = e.Item.ItemIndex;// EditItemIndex表示获取或设置 DataList 控件中要编辑的选定项的索引号。
// Rebind the data to the DataList
DataList1.DataBind();
}
第二个参数类型为DataListCommandEventArgs ,它是被点击的Edit button的DataListItem的引用(e.Item).首先设置DataList的EditItemIndex为需要编辑的DataListItem的ItemIndex,然后重新绑定数据。使DataList以只读模式展示item,需要:
1. 设置DataList的 EditItemIndex property 为一个不存在的DataListItem index -1是一个好的选择。(由于DataListItem index从0开始)
2. 重新绑定数据到DataList。由于没有DataListItem ItemIndex和DataList的EditItemIndex关联,整个DataList会展现为只读模式。
可以通过以下代码完成:
C#
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
// Set the DataList's EditItemIndex property to -1
DataList1.EditItemIndex = -1;
// Rebind the data to the DataList
DataList1.DataBind();
}
【实现更新功能】
完成UpdateCommand event handler,需要:
1.编程获取用户输入的product name和price,还有ProductID.
2.调用ProductsBLL类里的合适的UpdateProduct重载方法.
3.设置DataList的EditItemIndex property 为一个不存在的DataListItem index. -1 是一个好的选择。
4.重新帮订数据。
下面的代码完成了上面的功能:
C#
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
// Read in the ProductID from the DataKeys collection
int productID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
// Read in the product name and price values
TextBox productName = (TextBox)e.Item.FindControl("ProductName");
TextBox unitPrice = (TextBox)e.Item.FindControl("UnitPrice");//查找对应的控件
string productNameValue = null;
if (productName.Text.Trim().Length > 0)
productNameValue = productName.Text.Trim();
decimal? unitPriceValue = null;
if (unitPrice.Text.Trim().Length > 0)
unitPriceValue = Decimal.Parse(unitPrice.Text.Trim(), System.Globalization.NumberStyles.Currency);
// Call the ProductsBLL's UpdateProduct method...
ProductsBLL productsAPI = new ProductsBLL();
productsAPI.UpdateProduct(productNameValue, unitPriceValue, productID);//调用BLL中的重载方法
// Revert the DataList back to its pre-editing state
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
【删除功能】
为DataList的DeleteCommand事件创建一个event handler实现删除功能,见下面的代码:
C#
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
// Read in the ProductID from the DataKeys collection
int productID = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
// Delete the data
ProductsBLL productsAPI = new ProductsBLL();
productsAPI.DeleteProduct(productID);//调用BLL中的方法
// Rebind the data to the DataList
DataList1.DataBind();
}
所有功能实现完成,本章结束。
任务:关于BLL、DAL、ObjectDataSouce的层层调用,以及各自的参数传递问题,DataSet中的Adapter、DataTable直接的方法与查询,BLL中的函数重载问题,下次要做个专门的总结篇。
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" Width="355px"
DataKeyField="id"
oneditcommand="DataList1_EditCommand" onupdatecommand="DataList1_UpdateCommand"> <HeaderTemplate>
图片列表
</HeaderTemplate> <SelectedItemStyle BackColor="Red">
</SelectedItemStyle> <ItemTemplate>
图片 <%# DataBinder.Eval(Container.DataItem, "id") %>
<asp:LinkButton ID="LinkButton1" Text="Detail" CommandName="Edit" runat="server">Edit</asp:LinkButton>
</ItemTemplate> <EditItemTemplate>
图片ID
<asp:TextBox ID="txtid" runat="server" Text='<%# Eval("id") %>'></asp:TextBox>
<br />
图片路径
<asp:TextBox ID="txtimageUrl" runat="server" Text='<%# Eval("imageUrl") %>'></asp:TextBox>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update">Save</asp:LinkButton>
</EditItemTemplate> </asp:DataList> </div>
</form>
</body>
aspx.cs
public partial class _236EditData : System.Web.UI.Page
{
ShowImageBll showImageBll = new BLL.ShowImageBll();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
} private void BindDataList()
{
DataSet ds = showImageBll.GetList();
DataList1.DataSource = ds;
DataList1.DataBind();
}
private void UpadteDataList(int id,String imageUrl)
{
showImageBll.UpdateList(id,imageUrl); } protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
BindDataList();
} protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
// Read in the id from the DataKeys collection
int id = int.Parse(DataList1.DataKeys[e .Item .ItemIndex ].ToString());
string imageUrl = ((TextBox)e.Item.FindControl("txtimageUrl")).Text;// Read in the imageUr
UpadteDataList(id, imageUrl); // Call the ShowImageBll's UpadteDataLis method...
Response.Write("<script>alert('更新成功!')</script>");
DataList1.SelectedIndex = -;// Revert the DataList back to its pre-editing state
BindDataList(); }
}
【总结】:
1.EditCommand event — 当CommandName属性设为“Edit”的Button, LinkButton, 或 ImageButton 被点时激发.
CancelCommand event — 当CommandName属性设为“Cancel”的Button, LinkButton, 或ImageButton 被点时激发.
UpdateCommand event — 当CommandName属性设为“Update”的Button,LinkButton, 或ImageButton 被点时激发.
DeleteCommand event — 当CommandName属性设为“Delete”的Button, LinkButton, 或 ImageButton 被点时激发.
属性的名字必须设置为指定的Edit,Cancel ,Update,Delete
2. 设置编辑事件 oneditcommand="DataList1_EditCommand" 后台编辑事件里面的代码如下:
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
BindDataList();
}
《ASP.NET1200例》在DataList里编辑和删除数据的更多相关文章
- ASP.NET网页动态添加、更新或删除数据行
ASP.NET网页动态添加.更新或删除数据行 看过此篇<ASP.NET网页动态添加数据行> http://www.cnblogs.com/insus/p/3247935.html的网友,也 ...
- 《ASP.NET1200例》ListView 控件与DataPager控件的结合<二>
ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示 为什么使用ListView+DataPager的方式实现分页显示? .net提供的诸多数据绑定控件,每一种都有它自己 ...
- 《ASP.NET1200例》ListView 控件与DataPager控件的结合<一>
分页 在前一部分开始时介绍的原 HTML 设计中内含分页和排序,所以根据规范完整实现该网格的任务尚未完成.我们先分页,然后再排序. ListView 控件中的分页通过引入另一个新控件 Data ...
- 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定
aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...
- 《ASP.NET1200例》ASP.Net 之Datalist数据删除(支持批量)
.aspx <div> <asp:DataList ID="DataList1" runat="server" Width="355 ...
- 《ASP.NET1200例》<asp:DataList>分页显示图片
aspx页面代码 <asp:DataList ID="dlPhoto" runat="server" Height="137px" W ...
- 《ASP.NET1200例》<ItemTemplate>标签在html里面有什么具体的作用
严格的来说 <ItemTemplate> 在html中无意义,他只是针对诸如 Repeater.DataList.GridView中的一个模板 至于里面的含义,你可以这样想,既然Repea ...
- 《ASP.NET1200例》实现投票的用户控件
用户控件ascx <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="24 ...
- 《ASP.NET1200例》高亮显示ListView中的数据行并自动切换图片
aspx <script type="text/javascript"> var oldColor; function SetNewColor(Source) { ol ...
随机推荐
- Ibatis学习总结3--SQL Map XML 映射文件
在前面的例子中,只使用了 SQL Map 最简单的形式.SQL Map 的结构中还有其他更多 的选项.这里是一个 mapped statement 较复杂的例子,使用了更多的特性. <sqlMa ...
- 利用less监视模式实时预览样式刷新浏览器
[前言]此处介绍的方法只是我个人的用法,相信大家有更好更简洁的方式. 上次写到利用LiveReload解放F5.而且LiveReload可以编辑sass/less/stylus.但是可惜发现LiveR ...
- Java中唯一数的生成
唯一数的生成很简单,基本上以时间为基础进行生成.在JDK里面已经有java.util.UUID类可以生成唯一的随机数.如果希望生成的唯一数为特定的格式,那么就需要自己来生成唯一数了.生成唯一数时有两个 ...
- Json-转换
js转换 引用json.js(将json格式转换成字符串 var name = document.getElementById("name").value; var retries ...
- 【CodeForces 557B】Pasha and Tea
题 题意 总共有 w 克蛋糕,2n 个盘子,第 i 个盘子容量为 ai ,n 个女孩和 n 个男孩,男孩得到的是女孩得到的蛋糕的两倍,求他们得到蛋糕的最大值. 分析 把盘子从小到大排序,然后 女生得到 ...
- 【POJ 3176】Cow Bowling
题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...
- Ext comboBox的remote和local的区别
remote模式下不能使用模糊查询的功能 而local模式下可以实现模糊查询的功能 如果非要实现模糊查询的功能,最好就是提前把数据查询出来,缓存到本地,然后再用local模式 且,改个属性,改成可编辑 ...
- Sphinx学习之sphinx的安装篇
一. Sphinx简介 Sphinx是由俄罗斯人Andrew Aksyonoff开发的一个全文检索引擎.意图为其他应用提供高速.低空间占用.高结果 相关度的全文搜索功能.Sphinx可以非常容易的与 ...
- hdu 1008 Elevator
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description The hig ...
- Java基础之理解Annotation(与@有关,即是注释)
Java基础之理解Annotation 一.概念 Annontation是Java5开始引入的新特征.中文名称一般叫注解.它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata) ...