每次刷新页面时都会触发DataList按钮

时间:2020-04-04 21:44:29

标签: c# webforms datalist

我在购物车的数据列表中有一个链接按钮,它工作正常。他们选择产品后,如果刷新页面,它将重新触发此按钮。

    <asp:DataList runat="server" ID="rpCategories" RepeatColumns="4" RepeatDirection="Horizontal" OnItemCommand="rpCategories_ItemCommand"
            ItemStyle-HorizontalAlign="Center" ClientIDMode="Static" ItemStyle-CssClass="bigLink">
            <ItemTemplate>
                <table>
                    <tr>
                        <td>
                            <div class="<%=EnrollmentPacksBottomBox %>">
                                <div class="enrollment1ProImage"> 
                                    <img src="../../Common/Images/products/large/<%# Eval("largeImages") %>" width="250" />
                                </div>
                                <div class="enrollment1ProductName">
                                    <%# Eval("productName") %>
                                </div>
                                <div class="enrollment1DescriptionBottom1">
                                    <%# Eval("description") %>
                                </div>
                                <div class="enrollment1Price">
                                    <table>
                                        <tr>
                                            <td>
                                                <%# String.Format(CultureInfo.GetCultureInfo(MyCulture), "{0:c}", Eval("price")) %>
                                            </td>
                                            <td>
                                                <div class="enrollment1PvSpacer"></div>
                                            </td>
                                            <td>PV:
                                                <%# Eval("pv") %>
                                            </td>
                                        </tr>
                                    </table>  <%-- new amount, and buttons- start--%>
                                    <table class="cartButtonsHolder">
                                        <tr>    
                                            <td>
                                                <div class="">
                                                    <button type="button" style="border: none; background-color: white;">
                                                    <table class="cartButtons">
                                                        <tr>
                                                            <td rowspan="2">
                                                                <asp:TextBox runat="server" ID="quantityHolder" Text='<%# Eval("quantity") %>' CssClass="QuantityBox"/>
                                                            </td>
                                                            <td>
                                                                <asp:LinkButton runat="server" ID="upButton" OnClick="AddItem" CssClass="caretButton fas fa-caret-up" Text="" />
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                <asp:LinkButton runat="server" ID="downButton" OnClick="SubtractItem" CssClass="caretButton fas fa-caret-down fa-m" Text="" />
                                                            </td>
                                                        </tr>
                                                    </table>
                                                    </button>
                                                </div>
                                            </td>
                                            <td>
                                                <asp:LinkButton runat="server" OnClick="SelectPro1Bottom"><div class="enrollment1SelectButton"> Add to Cart</div></asp:LinkButton>
                                            </td>
                                        </tr>
                                </div>
                            </div>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>

后面的代码如下:

    protected void SelectPro1Bottom(object sender, EventArgs e)
    {
        DAL oDal = new DAL();
        var btn = (LinkButton)sender;
        //var btn = (HtmlButton)sender;
        var item = (DataListItem)btn.NamingContainer;  //RepeaterItem
        int index = item.ItemIndex;
        int quantity = 0;

        GetSelected(index);
        Session["amount"] = 1;
        //pull in the quantity
        Quantities = Session["formatQuantities"] as List<int>;
        List<OrderUserTypeHolder> TempHolder = new List<OrderUserTypeHolder>();
        OrderUserTypeHolder oh = new OrderUserTypeHolder();

        TempHolder = Session["userTypeHolder"] as List<OrderUserTypeHolder>;

        int userType = 0;
        int hasAutoship = 0;
        string country = "";
        userType = TempHolder[0].UserType;
        hasAutoship = TempHolder[0].HasAutoship;
        country = TempHolder[0].Country;

        ProductIdList.Clear();

        using (oDal)
        {
            //dist
            if (userType == 2 && hasAutoship == 1)
            {
                SqlDataReader rd = oDal.ProductsGetAllByDescLengthAutoReader(country);

                while (rd.Read())
                {
                    ProductIdList.Add(Convert.ToInt32(rd["productId"]));
                }
            }
            else if (userType == 2)
            {
                SqlDataReader rd = oDal.ProductsGetAllByDescLengthDistReader(country);

                while (rd.Read())
                {
                    ProductIdList.Add(Convert.ToInt32(rd["productId"]));
                }
            }
            //pc
            else if (userType == 3)
            {
                SqlDataReader rd = oDal.ProductsGetAllByDescLengthPcReader(country);

                while (rd.Read())
                {
                    ProductIdList.Add(Convert.ToInt32(rd["productId"]));
                }
            }
            //retail
            else
            {
                SqlDataReader rd = oDal.ProductsGetAllByDescLengthRetailReader(country);

                while (rd.Read())
                {
                    ProductIdList.Add(Convert.ToInt32(rd["productId"]));
                }
            }
            oDal.Dispose();
        }

        //make sure we don't have an empty array
        if (Quantities.Count > 0)
        {
            quantity = Quantities[index];
        }
        else
        {
            quantity = 1;
        }

        int productId = ProductIdList[index];
        AddItemsToCart(productId, quantity, hasAutoship);
    }

我尝试了多种修复方法,包括将null值提供给数据源,更改视图状态并将其移入!isPostback内(链接按钮不起作用)。每次单击链接按钮时,它都会重新加载页面(不会进入!isPostback),刷新页面的效果相同。每次重新刷新页面时,它将在购物车中添加另一个最后选择的项目。在过去的几个小时中,我尝试了所有我能想到的东西以及在网上找到的所有东西。

任何建议将不胜感激!

0 个答案:

没有答案
相关问题