首页 > 解决方案 > 如何从同一网格视图中的按钮的单击事件中设置 GridView 中标签的文本值

问题描述

我在 gridview 中有一个按钮和一个无界标签(用于库存数量级别标志)。(其他列有界)。当我单击按钮时,我希望 CLICKED ROW 的标签显示:根据条件有货或缺货。但是现在所有行的所有标签都显示相同的东西。

按钮单击事件中的代码检查数量(条件)级别并且工作正常。我还能够从 RowDataBound 中的 gridview 中找到标签并根据数量条件设置文本属性,但无法让该标签和文本属性仅显示在单击的行中。

Private Sub CartGrid_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles CartGrid.RowDataBound

 If (e.Row.RowType = DataControlRowType.DataRow) Then
        If ProdQtyInStock = 0 Then
            Dim lblCartQtyMsg As Label = CType(e.Row.FindControl("lblCartQtyMsg"), Label)
            lblCartQtyMsg.Text = "Out of Stock"

        End If
    End If
 End Sub

我希望标签只显示在单击按钮的行上。这是按钮单击事件的代码:

Protected Sub btninc_Click(ByVal sender As Object, ByVal e As  System.EventArgs)
   
    Dim PlusButton As Button = DirectCast(sender, Button)
    Dim Row As GridViewRow = DirectCast(PlusButton.Parent.Parent, GridViewRow)
    Dim productid As String =     CartGrid.DataKeys(Row.RowIndex).Value.ToString
    Dim lblcartSize As Label = CType(Row.FindControl("lblcartSize"), Label)
    Dim lblcartcolor As Label = CType(Row.FindControl("lblcartColor"), Label)
    Dim lblCartQty As Label = CType(Row.FindControl("LblCartQty"), Label)
    Dim cartQty As String
    If lblcartSize.Text.ToString = "NIL" And lblcartcolor.Text.ToString = "NIL" Then
        Dim colorid, sizeid As Integer
        sizeid = 1
        colorid = 1
        ProdQtyInStock = ShoppingCart.StockQtyChecker(productid, sizeid, colorid)
      If ProdQtyInStock = 1 Then
            ShoppingCart.UpdateQtyIncrement(productid, sizeid, colorid)
      ElseIf ProdQtyInStock = 0 Then
            ' show lblCartQtyMsg.Text = "Out of Stock"

        End If
        ' bindShoppingCart()
        ReCalculateShippingCost()
     Else
        Dim colorid, sizeid As Integer
        colorid = ShoppingCart.GetColorIDforProdCartDelete(productid, lblcartcolor.Text.ToString).colorid
        sizeid = ShoppingCart.GetSizeIDforProdCartDelete(productid, lblcartSize.Text.ToString).sizeid

        ProdQtyInStock = ShoppingCart.StockQtyChecker(productid, sizeid, colorid)

        If ProdQtyInStock = 1 Then
            ShoppingCart.UpdateQtyIncrement(productid, sizeid, colorid)
        ElseIf ProdQtyInStock = 0 Then

            'at show lblCartQtyMsg.Text = "Out of Stock"

        End If

        '' bindShoppingCart()
        ReCalculateShippingCost()
                 
    End If
    
    cartQty = lblCartQty.Text

End Sub

这是按钮的html:

<ItemTemplate>
            <asp:Button ID="btnIncrement" runat="server"     CausesValidation="false" CommandName="Increment"
                 Text="+"   OnClick ="btninc_Click"  Font-Bold="True"     style="background-color:#fff; border :solid 1px #ccc"  />
        </ItemTemplate>

这是购物车目前的样子。

在此处输入图像描述

标签: asp.netvb.net

解决方案


推荐阅读