DataList中的相关操作,获取当前编辑行中某列的值

本文介绍如何使用ASP.NET中的DataList控件进行数据展示及增删改查等操作,并详细解释了如何通过绑定事件来实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(将自己平时学到的东西谨在此做个记录,以备以后查询参考之用) 

在后台代码编辑之前,需要设置:

1. DataList的ID:dlCategory, DataKeyField="CategoryID"

2. Item Templat中 ,两个label分别显示(Text属性),CategoryID,CategoryName

    Item Templat中 ,两个TextBox分别设置(ID属性),txtCategoryID,txtCategoryName,另外需要绑定数据,即在EditDataBinding中的Text项输入:DataBinder.Eval(Container. DataItem,"CategoryID")/Eval("CategoryID")

    Item Templat中 ,四个个Button分别设置。ID属性,Text属性,CommandName属性(important)

    select,update,cancle,delete.在dlCategory_ItemCommand()方法中,用于判断执行的操作。

3.该代码还需用到MVC三层架构的东西,参考MVC文章。

 

代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
//更新页面数据
}
}

private void Bind() //定义方法,加载数据源,绑定数据
{
ItemCategoryBLL categoryBLL
= new ItemCategoryBLL(); //实例化BLL
dlCategory.DataSource = null;
dlCategory.DataSource
= categoryBLL.GetAllCategoryList(); //加载数据源
dlCategory.DataBind(); //绑定数据
}

protected void dlCategory_ItemCommand(object source, DataListCommandEventArgs e)//在ItemCommand方法中写入
{
if(e.CommandName=="update")
{
ItemCategoryBLL categoryBLL
=new ItemCategoryBLL();
dlCategory.EditItemIndex
=e.Item.ItemIndex;
string categoryName = ((TextBox)e.Item.FindControl("txtCategoryName")).Text;
//获取当前编辑行的CategoryName列中的数据
int categoryID = Convert.ToInt32(((TextBox)e.Item.FindControl("txtCategoryID")).Text);
//获取当前编辑行的CategoryID列中的数据
categoryBLL.UpdateCategory(categoryName,categoryID);//实例化后的BLL调用Update方法
Bind(); //重新更新页面数据
}
else if (e.CommandName=="cancle")
{
dlCategory.EditItemIndex
= -1;//设置DataList控件的编辑项的索引为-1,取消编辑
Bind();
}
else if (e.CommandName == "delete")
{
ItemCategoryBLL categoryBLL
= new ItemCategoryBLL();
dlCategory.EditItemIndex
= e.Item.ItemIndex;
int categoryID = Convert.ToInt32(((TextBox)e.Item.FindControl("txtCategoryID")).Text);
int i= categoryBLL.DeleteCategory(categoryID);
if(i!=0)
{
Bind();
}

 

转载于:https://www.cnblogs.com/eva_2010/articles/1828253.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值