循环遍历asp:repeater项目,仅更改第一项

时间:2015-10-29 03:53:51

标签: asp.net vb.net

我正在为网站创建折扣券,购物车是Databound ASP:Repeater。

问题 我无法在此代码上使用内置调试器,因为它是一个实时托管的IIS网站(就是我所说的,我“我很漂亮”,调试器只是在尝试运行它时才会哭。没有任何东西到达SQL服务器并且超链接不起作用,并且此特定页面将始终打开到空的购物车。

我有几张优惠券,但有一种类型无法正常使用。我想拿一份csv的股票代码,并检查产品的每个实例的购物车。

示例字符串:SP08075,sp08076,sp08077,sp08078,sp08079,sp08098

但我的凭证只会影响购物车中的第一个匹配项目,而忽略其余部分。

这是我的转发器的项目模板:

<itemtemplate>

    <div class="cart-product-row">
        <div class="cart-product-id"><asp:HyperLink ID="hlProductID" runat="Server"></asp:HyperLink></div>
    <div class="cart-product-holder">
        <div class="cart-product"><asp:HyperLink ID="hlProductName" runat="server"></asp:HyperLink></div>
        <div class="cart-options">
            <input name="hidden" type="hidden" id="UniqueValue" value="" runat="server" />
            <input name="hiddencat" type="hidden" id="Category" value="" runat="server" />
            <div id="divOption" runat="server" Visible="false">
                <asp:Label ID="lblOptionsName" runat="server"></asp:Label>
            </div>
        </div>
    </div>  
    <div class="cart-unit-price">$<asp:Label ID="lblPrice" runat="server"></asp:Label></div>
    <div class="cart-quantity"><asp:TextBox onfocus="this.select();" onkeypress="return noenter();" AutoPostBack="true" ID="txtQuantity" runat="server" Width="30" CssClass="textbox"></asp:TextBox></div>
    <div class="cart-price-total">$<asp:Label ID="lblTotalPrice" runat="server"></asp:Label></div>
    <div class="cart-remove"><asp:LinkButton ID="btnDelete" CommandName="Delete" Text="Remove" runat="server"></asp:LinkButton></div>
</div>
<div class="cart-oos-liner"><asp:Label ID="lblOOSWarning" Visible="false" ForeColor="Red" runat="server"></asp:Label></div> 

这是采用该字符串并尝试折扣正确代码的方法。

VB.Net

    Private Sub RunStockVoucher()           'Voucher applies only to specified stock codes.
    Dim row As DataRow = vData.Rows(0)  'Get voucher data from DataTable

    'Declare variables relevant to this voucher
    Dim discount As Int32 = row("Discount")
    Dim dollarPercent As Int32 = row("DollarPercent")
    Dim stockValidCodes As String = row("stockvalidcodes")
    Dim stockAllSome As Int32 = row("stockAllSome")
    Dim stockSomeNo As Int32 = row("stockSomeNo")
    Dim codes As String() = stockValidCodes.Split(",")      'If more than one stock code is valid, split the string into an Array and remove the comma separator
    Dim num As Int32 = 0                                    'Set number to 0 for further checking.


    'For Each code As String In codes                        'Check each valid stock code agains the whole cart.
    For Each item As RepeaterItem In rptCart.Items      'Loop through every item.
        For Each code As String In codes
            'Get item properties
            Dim price As Label = CType(item.FindControl("lblPrice"), Label)
            Dim cost As Single = CSng(price.Text)
            Dim UniqueCode As HtmlInputHidden = item.FindControl("UniqueValue")
            Dim prodID As HyperLink = CType(item.FindControl("hlProductID"), HyperLink)
            Dim qty As TextBox = CType(item.FindControl("txtQuantity"), TextBox)
            Dim quant As Int32 = Int32.Parse(qty.Text)
            Dim extCode As String = New Guid().ToString()

            'If product Stock Code matches valid code run further checks, else skip to next product.
            If String.Equals(code, prodID.Text.ToString().Trim()) = True Then
                If stockAllSome = 0 Then        '0 = Discount applies to all instances of valid stock.
                    c.UpdateCartObject(prodID.Text, UniqueCode.Value, 0)
                    If dollarPercent = 0 Then       'Apply discounts, 0= Dollar, 1 = Percentage, 2 = Fixed Price
                        c.AddCartObject(prodID.Text, UniqueCode.Value, Product.GetProductNameByCode(prodID.Text), quant, cost - discount)
                    ElseIf dollarPercent = 1 Then
                        Dim perc As Single = discount / 100
                        Dim perc2 As Single = 1 - perc
                        c.AddCartObject(prodID.Text, UniqueCode.Value, Product.GetProductNameByCode(prodID.Text), quant, cost * perc2)
                    ElseIf dollarPercent = 2 Then
                        c.AddCartObject(prodID.Text, UniqueCode.Value, Product.GetProductNameByCode(prodID.Text), quant, discount)
                    End If
                Else        'ELSE discount only applies to some of the items.
                    If quant > stockSomeNo Then 'If there are more than the valid amount of the item, split into two cart items.
                        c.UpdateCartObject(prodID.Text, UniqueCode.Value, 0)    'First item is discounted, at valid quantity
                        If dollarPercent = 0 Then
                            c.AddCartObject(prodID.Text, UniqueCode.Value, Product.GetProductNameByCode(prodID.Text), stockSomeNo, cost - discount)
                        ElseIf dollarPercent = 1 Then
                            Dim perc As Single = discount / 100
                            Dim perc2 As Single = 1 - perc
                            c.AddCartObject(prodID.Text, UniqueCode.Value, Product.GetProductNameByCode(prodID.Text), stockSomeNo, cost * perc2)
                        ElseIf dollarPercent = 2 Then
                            c.AddCartObject(prodID.Text, UniqueCode.Value, Product.GetProductNameByCode(prodID.Text), stockSomeNo, discount)
                        End If

                        'Second cart item is not discounted, quantity set to remainder amount.
                        c.AddCartObject(prodID.Text, extCode, Product.GetProductNameByCode(prodID.Text), quant - stockSomeNo, cost)
                    Else    'If quantity is less than valid number, discount all items.
                        c.UpdateCartObject(prodID.Text, UniqueCode.Value, 0)
                        If dollarPercent = 0 Then
                            c.AddCartObject(prodID.Text, UniqueCode.Value, Product.GetProductNameByCode(prodID.Text), quant, cost - discount)
                        ElseIf dollarPercent = 1 Then
                            Dim perc As Single = discount / 100
                            Dim perc2 As Single = 1 - perc
                            c.AddCartObject(prodID.Text, UniqueCode.Value, Product.GetProductNameByCode(prodID.Text), quant, cost * perc2)
                        ElseIf dollarPercent = 2 Then
                            c.AddCartObject(prodID.Text, UniqueCode.Value, Product.GetProductNameByCode(prodID.Text), quant, discount)
                        End If
                    End If
                End If
            End If
        Next
    Next
    c.UpdateUser(c.CustomerID)  'Apply changes on Customer level (GPCUSer.vb), bind data to display changes on screen
    c.VoucherName = row("Name")
    BindData()
End Sub

每个循环将成功地折扣第一个匹配代码,例如它在购物车中找到的SP08075,但无论有多少其他股票代码匹配,都没有变化。

我不认为价格调整的显示方式是错误的,因为我有另一张优惠券,无论股票代码如何都会折扣每一个购物车商品,它会循环并折扣一切就好了。我认为这与'string.equals'设置有关,但我无法弄清楚问题是什么。

1 个答案:

答案 0 :(得分:1)

似乎是字符串大小写问题。可以像这样使用45 10 * * * Documents/test.py

ToLower()