prashant dhuri
prashant dhuri

Reputation: 61

How to access Label inside DataList which is inside another DataList

There are 2 DataList DataList2 and DataList3
DataList2 has DataList3 and a Button and lblOrderID
DataList3 has lblQuantity
On Click of Button value of lblQuantity should be assigned to qty
When i debug this code it shows qty is null?
Error : Object reference not set to an instance of an object.

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);
    }
}

Upvotes: 0

Views: 918

Answers (1)

VDWWD
VDWWD

Reputation: 35554

The problem is in this line:

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

Although you use an individual DataListItem to find the nested DataList (with the NamingContainer), you then proceed to find a Label in DataList3 itself, not an Item in DataList3.

It should be

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

Upvotes: 1

Related Questions