如何访问位于另一个 DataList 内的 DataList 内的 Label

How to access Label inside DataList which is inside another DataList

有 2 个 DataList DataList2DataList3
DataList2DataList3 和一个 ButtonlblOrderID
DataList3lblQuantity
单击 Button 时,lblQuantity 的值应分配给 qty
当我调试此代码时它显示数量为空?
错误:未将对象引用设置为对象的实例。

protected void bremove_Click(object sender, EventArgs e)
{
    Button remove = (Button)sender;
    DataListItem row = remove.NamingContainer as DataListItem;
    DataList dat = (DataList)row.FindControl("DataList3");
    Label qty = (Label)dat.FindControl("lblQuantity");
    Label id = (Label)row.FindControl("lblOrderID");
    string oid = id.Text;
    string oqty = qty.Text;
    sqlqueries.UpdateOrder(oid, oqty);
    int k = sqlqueries.CancelOrder(oid);
    if (k != 0)
    {
        Response.Redirect(Request.RawUrl);
    }
}

问题出在这一行:

Label qty = (Label)dat.FindControl("lblQuantity");

尽管您使用单独的 DataListItem 来查找嵌套的 DataList(使用 NamingContainer),但您随后会继续在 DataList3 本身中查找标签,而不是 中的项目DataList3.

应该是

Label qty = (Label)dat.Items[row.ItemIndex].FindControl("lblQuantity");