Idrees Khan
Idrees Khan

Reputation: 7752

Asp.Net Get Control Values from PlaceHolder

I have defined a placeholder in my page like this;

<asp:PlaceHolder ID="attrPlaceHolder" runat="server"></asp:PlaceHolder> 

I am populating this place holder from a database table using query string productId like this;

 // obtain the attributes of the product
                DataTable attrTable = CatalogAccess.GetProductAttributes(productId);
                // temp variables
                string prevAttributeName = "";
                string attributeName, attributeValue, attributeValueId;
                // current DropDown for attribute values
                Label attributeNameLabel;
                DropDownList attributeValuesDropDown = new DropDownList();
                // read the list of attributes
                foreach (DataRow r in attrTable.Rows)
                {
                    // get attribute data
                    attributeName = r["AttributeName"].ToString();
                    attributeValue = r["AttributeValue"].ToString();
                    attributeValueId = r["AttributeValueID"].ToString();
                    // if starting a new attribute (e.g. Color, Size)
                    if (attributeName != prevAttributeName)
                    {
                        prevAttributeName = attributeName;
                        attributeNameLabel = new Label();
                        attributeNameLabel.Text = "<li class=\"txt\">" + attributeName + ":</li>";
                        attributeValuesDropDown = new DropDownList();
                        attrPlaceHolder.Controls.Add(attributeNameLabel);
                        attrPlaceHolder.Controls.Add(attributeValuesDropDown);
                    }
                    // add a new attribute value to the DropDownList
                    attributeValuesDropDown.Items.Add(new ListItem(attributeValue, attributeValueId));
                }

However, when inside a button click event, when I loop through this place using visual studio debugging, I saw that the visual studio studio debugger first hit the "attrPlaceHolder.Controls" word in my foreach loop, then secondly comes to 'in' keyword (in foreach loop) but it isn't hitting the first two words (i-e 'Control cnt' in my foreach loop. Here it looks;

protected void ButtonBuyNow_Click(object sender, EventArgs e)
{
    // Retrieve the selected product options
    string options = "";
    foreach (Control cnt in attrPlaceHolder.Controls)
    {
        if (cnt is Label)
        {
            Label attrLabel = (Label)cnt;
            options += attrLabel.Text;
        }

        if (cnt is DropDownList)
        {
            DropDownList attrDropDown = (DropDownList)cnt;
            options += attrDropDown.Items[attrDropDown.SelectedIndex] + "; ";
        }
    }

    // Add the product to the shopping cart
    ShoppingCartAccess.AddItem(productId, options);
}

Basically I need 'options' variable to be populated but it isn't hitting the foreach loop inside, therefore I am not able to get the 'options' variable populated. This is a serious problem in my application. Please tell me why I can't get the inside the foreach loop.

NOTE: please note that this isn't the complete code of my entire page. My rest of the code executes correctly.

Upvotes: 1

Views: 9960

Answers (1)

Andre Calil
Andre Calil

Reputation: 7692

why I can't get the inside the foreach loop

Because the list is empty.

Why is the list empty? (Would be the next logical question)

Because, at ASP.Net, dynamically created controls must be re-created at Page_Init in order to exist. When you create them at this stage, the page lifecycle will bind the viewstate and will be ready for use. If you receive a postback (from the button, for example) and don't recreate them, they simply don't exist.

Upvotes: 4

Related Questions