0

我有nested-datalist孩子datalistitemcommand事件我正在做这样的事情

if (e.CommandName == "Delete")
{
    string keyID;

    int idx = e.Item.ItemIndex;
    DataClasses1DataContext db = new DataClasses1DataContext();
    DataList dl = Session["dl"] as DataList;
    object thisKey = dl.DataKeys[idx];

    keyID = thisKey.ToString();
    foreach (DataListItem item in dl.Items)
    {
        LinkButton lb = item.FindControl("LinkButton1") as LinkButton;
        ImageButton ib = item.FindControl("ImageButton1") as ImageButton;
        string s = item.ItemIndex.ToString();
        string j = s;
        if (item.ItemIndex == idx)
        {
           string dds = ib.AlternateText;
           Label ServiceCommentIDLabel = item.FindControl("ServiceCommentIDLabel") as Label;
           string ds = ServiceCommentIDLabel.Text;
           ServiceComment sc = db.ServiceComments.Where(o => o.ServiceCommentID == long.Parse(ServiceCommentIDLabel.Text)).First();
           db.ServiceComments.DeleteOnSubmit(sc);
           Response.Redirect("Services.aspx");
        }
     }
 }

它既不是选择精确的,datakey也不是foreach遍历每个item.datalist

4

1 回答 1

0

DataList从中得到Session什么不好的东西。您必须在这样的事件中DataListObject源转换:ItemCommand

protected void Child_DataList_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        // get DataList from the source
        DataList dl = source as DataList;

        object thisKey = dl.DataKeys[e.Item.ItemIndex];
        // do your work here
    }
}
于 2017-06-28T13:54:26.300 回答