Skip to main content
 首页 » 编程设计

c#之Asp.Net 从 PlaceHolder 获取控件值

2024年02月20日13mengfanrong

我在我的页面中定义了一个占位符,如下所示;

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

我像这样使用查询字符串 productId 从数据库表中填充这个占位符;

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

但是,在按钮单击事件中,当我使用 visual studio 调试循环遍历此位置时,我看到 visual studio studio 调试器首先在我的 foreach 中点击了“attrPlaceHolder.Controls” 字样循环,然后是 'in' 关键字(在 foreach 循环中),但它没有命中前两个词(即在我的 foreach 循环中的 'Control cnt'。看这里;

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

基本上我需要填充“options”变量,但它没有触发内部的 foreach 循环,因此我无法填充“options”变量。 这是我的应用程序中的一个严重问题。请告诉我为什么我无法进入 foreach 循环。

注意: 请注意,这不是我整个页面的完整代码。我的其余代码正确执行。

请您参考如下方法:

why I can't get the inside the foreach loop

因为列表是空的。

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

因为在 ASP.Net 中,动态创建的控件必须在 Page_Init 重新创建才能存在。当您在此阶段创建它们时,页面生命周期 将绑定(bind) View 状态并可供使用。 如果您收到回发(例如,来自按钮)并且不重新创建它们,那么它们根本不存在。