我有nested-datalist,在子datalist,itemcommand事件上,我做了一些如下所示的事情
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,也不是遍历datalist的每个item的foreach循环。
发布于 2017-06-28 21:54:26
你从Session得到了DataList,这不是一件好事。您必须在ItemCommand事件中转换来自Object源代码的DataList,如下所示:
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
}
}https://stackoverflow.com/questions/24133747
复制相似问题