Using foreach loop with DataList on Page Load

Joe W picture Joe W · May 16, 2013 · Viewed 9.2k times · Source

I am working with a datalist on one of my pages, while trying to find the controls using a foreach loop it does not go into the loop.

protected void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
        foreach (DataListItem dli in shoplistview.Items)
        {
            string productID = ((Label)dli.FindControl("lblProductID")).Text;
        }
    }
}

Is the datalist not in the current runtime when !IsPostBack? Or is there something I am missing here? If you need any additional info I can try to include it. I appreciate any advice you can offer to me. Thank you for your time.

Answer

Abhishek B Patel picture Abhishek B Patel · May 16, 2013

Bind Datalist[shoplistview] before the for Loop