动态创建的按钮甚至可以在updatepanel内部进行回发

时间:2018-04-03 06:33:16

标签: c# html asp.net

我的按钮即使在更新面板内也会继续触发回发。我之前能够以相同的方法在同一个项目上工作,并且工作得很好,我不确定为什么它在这个项目中不起作用。

客户端代码

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"/>
<div class="shop-cart-body">
        <asp:UpdatePanel runat="server" UpdateMode="Always">
            <ContentTemplate>
                <asp:Panel runat="server" CssClass="shop-cart-item-container" ID="shopItemContainer">

                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>

CodeBehind这是按钮和其他东西来自

public void LoadProducts()
    {
        ShopProduct sProduct = new ShopProduct();
        List<Product> products = sProduct.LoadProductJson();

        foreach (var product in products)
        {
            Panel productPanel = new Panel { CssClass = "shop-plan-product" };
            Panel pricePanel = new Panel { CssClass = "product-price" };
            Panel discountPanel = new Panel { CssClass = "product-discount" };
            Label titleLbl = new Label { Text = product.Title, CssClass = "product-title" };
            Label priceLbl = new Label { Text = string.Format("{0:n0}", ((product.Quantity * product.Price) - ((product.Quantity * product.Price) * product.Discount))) };
            Label pricedecLbl = new Label { Text = ".00/YR" };
            Label discountperLbl = new Label { Text = (product.Discount * 100).ToString() };
            Label discountLbl = new Label { Text = "% Discount" };
            Image pesoImg = new Image { ImageUrl = "~/Content/Images/peso.png" };
            Button cartBtn = new Button { Text = "Add to Cart", ID = "cbtn" + product.ProductID };
            cartBtn.Click += cartBtn_Click;
            BulletedList featuresBl = new BulletedList();


            ListItem featuresli1 = new ListItem { Text = "Unlimited teleconsultation" };
            int months = product.ValidationPeriod / 30;
            int years = months / 12;
            ListItem featuresli2 = new ListItem();
            if (years < 1)
            {
                if (months > 1)
                {
                    featuresli2.Text = months + " months access";
                }
                else if (months == 1)
                {
                    featuresli2.Text = months + " month access";
                }
                else
                {
                    featuresli2.Text = product.ValidationPeriod + " days access";
                }
            }
            else
            {
                if (months % 12 == 0)
                {
                    if (years == 1)
                    {
                        featuresli2.Text = years + " year access";
                    }
                    else
                    {
                        featuresli2.Text = years + " years access";
                    }
                }
                else
                {
                    months = months % 12;

                    if (years == 1)
                    {
                        if (months == 1)
                        {
                            featuresli2.Text = years + " year " + months + " month access";
                        }
                        else
                        {
                            featuresli2.Text = years + " year " + months + " months access";
                        }

                    }
                    else
                    {
                        if (months == 1)
                        {
                            featuresli2.Text = years + " years " + months + " month access";
                        }
                        else
                        {
                            featuresli2.Text = years + " years " + months + " months access";
                        }
                    }

                }
            }


            ListItem featuresli3 = new ListItem { Text = "Good for " + product.Quantity + " person" };
            ListItem featuresli4 = new ListItem { Text = "24/7 teleconsultation" };
            ListItem featuresli5 = new ListItem { Text = "Medicine delivery" };
            ListItem featuresli6 = new ListItem { Text = "Medical certificate" };

            pricePanel.Controls.Add(pesoImg);
            pricePanel.Controls.Add(priceLbl);
            pricePanel.Controls.Add(pricedecLbl);

            discountPanel.Controls.Add(discountperLbl);
            discountPanel.Controls.Add(discountLbl);

            featuresBl.Items.Add(featuresli1);
            featuresBl.Items.Add(featuresli2);
            featuresBl.Items.Add(featuresli3);
            featuresBl.Items.Add(featuresli4);
            featuresBl.Items.Add(featuresli5);
            featuresBl.Items.Add(featuresli6);

            productPanel.Controls.Add(titleLbl);
            productPanel.Controls.Add(pricePanel);
            productPanel.Controls.Add(discountPanel);
            productPanel.Controls.Add(cartBtn);
            productPanel.Controls.Add(featuresBl);

            shopPlanProductContainer.Controls.Add(productPanel);

        }

    }

这是点击功能

的地方
protected void cartBtn_Click(object sender, EventArgs e)
    {
        int id = Int32.Parse(((Button)sender).ID.Substring(4));

        List<CartItem> cartItems = new List<CartItem>();

        cartItems = (List<CartItem>)HttpContext.Current.Session["MedCart"];

        CartItem cartItem = new CartItem();
        cartItem.ProductID = id;
        cartItem.Quantity = 1;

        cartItems.Add(cartItem);

    }

希望有人能为这长篇代码感到抱歉,我完全理解这一点。

1 个答案:

答案 0 :(得分:0)

您必须使用更新模式作为条件和另一个属性ChildrenAsTriggers。试试下面的内容。

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"/>
<div class="shop-cart-body">
        <asp:UpdatePanel runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Panel runat="server" CssClass="shop-cart-item-container" ID="shopItemContainer">

                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>