数据列表UpdateCommand不起作用 [英] Datalist UpdateCommand not working

查看:57
本文介绍了数据列表UpdateCommand不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
以下是dataList控件(Datalist1)的EditTemplete-UpdateCommand的代码,我正在使用Linq2Sql. Sql类的名称是Baby,我正在使用datalist控件来显示Baby表中的所有记录.
问题是,对于更新,我如何获取要更新的记录的Itemindex.我知道问题仅在于以下代码中的一行:"Baby bab =(Baby)e.Item.DataItem;"但我真的不知道该怎么做

Hey Guys,
following is the code for EditTemplete-UpdateCommand of a dataList Control(Datalist1), I am using Linq2Sql. The Sql Class name is Baby and I am using the datalist control to diplay the all the records from Baby table.
The problem is, that for updating how do I get the Itemindex of the record that is to be updated. I know the problem is only with one line in the following code that is "Baby bab = (Baby)e.Item.DataItem;" but I really don''t know how to get it right

protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
    TextBox txtName = (TextBox)e.Item.FindControl("txtName");
    TextBox txtDate = (TextBox)e.Item.FindControl("txtDate");
    TextBox txtTime = (TextBox)e.Item.FindControl("txtTime");
    TextBox txtParents = (TextBox)e.Item.FindControl("txtParents");
    TextBox txtWeight = (TextBox)e.Item.FindControl("txtWeight");
    TextBox txtLength = (TextBox)e.Item.FindControl("txtLength");

    Baby bab = (Baby)e.Item.DataItem;  ////Code with the problem
    bab.name = txtName.Text;
    bab.date = txtDate.Text;
    bab.time = txtTime.Text;
    bab.parents = txtParents.Text;
    bab.weight = txtWeight.Text;
    bab.length = txtLength.Text;
    bab.Save();
    DataList1.EditItemIndex = -1;
    DataBind();



谢谢您的帮助.
Chetan.



Thank you for all the help.
Chetan.

推荐答案

使用DataList的DataKeyField属性指示Baby对象的主键.

< asp:DataList ... DataKeyField ="BabyPkId" ...

从数据键中检索主键

Use DataKeyField property of the DataList indicate primary key of your Baby object.

<asp:DataList ... DataKeyField="BabyPkId" ...

Retrieve the primary key from the datakeys

int pkId = (int)DataList1.DataKeys[(int)e.Item.ItemIndex];



在Linq2Sql数据源中查询与pkId中的值匹配的Baby实体,设置属性并更新.



Query your Linq2Sql datasource for the Baby entity matching the value in pkId, set the properties, and update.


这篇关于数据列表UpdateCommand不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆