开发者

ASP.NET Gridview Linkbutton Hiding/Display page load issue

I am trying to hide/display a linkbutton in my gridview based on wether not the row in the Gridview is part of my shopping cart.

This is开发者_开发问答 the gridview

    <asp:GridView ID="productListTable" runat="server" DataSourceID="srcProductListPerCustomer" AutoGenerateColumns="False" AlternatingRowStyle-CssClass="tr_dark"  HeaderStyle-CssClass="header_req" BorderWidth="0px" GridLines="None" AllowPaging="true" PageSize="25" EmptyDataText="No records." AllowSorting="false" Width="100%" DataKeyNames="product_ID_key" OnRowDataBound="productListTable_RowDataBound" OnRowCommand="productListTable_RowCommand"  >         
    <Columns>                         
            <asp:TemplateField HeaderText="Product Name" HeaderStyle-Width="250px" SortExpression="productName" ItemStyle-CssClass="product_name" >
                <ItemTemplate>   
                <asp:Label ID="ProductNameField" runat="server" Text='<%# Eval("productName").ToString() %>'></asp:Label>
                </ItemTemplate>                     
            </asp:TemplateField> 

    </Columns>
     <Columns>  

    <asp:TemplateField HeaderText="Quantity">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="txtQuantity" Columns="5"></asp:TextBox><br />
                        <asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("product_ID_key") %>' style="font-size:12px;" Visible="false"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
    </Columns>
    <HeaderStyle CssClass="header_req" />
    <AlternatingRowStyle CssClass="tr_dark" />
    <PagerStyle CssClass="pagination" />
    <PagerSettings PageButtonCount="3" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" Mode="NumericFirstLast" />      
</asp:GridView>

The way I am trying to do this at the moment is by row data bound

     Protected Sub productListTable_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles productListTable.RowDataBound
    ' If we are binding the footer row, let's add in our total
    If e.Row.RowType = DataControlRowType.DataRow Then
        'e.Row.Cells(6).Text = "Total Price: " & ShoppingCart.Instance.GetSubTotal().ToString("C")
        Dim productId = Convert.ToInt32(productListTable.DataKeys(e.Row.RowIndex).Value)

        Dim txtQuantity As TextBox = CType(e.Row.Cells(1).FindControl("txtQuantity"), TextBox)
        Dim removeButton As LinkButton = CType(e.Row.Cells(1).FindControl("btnRemove"), LinkButton)

        Dim updatedItem = New CartItem(productId)

        For Each item As CartItem In ShoppingCart.Instance.Items
            If item.Equals(updatedItem) Then
                txtQuantity.Text = item.Quantity
                removeButton.Visible = True
            End If
        Next
    End If
End Sub

And it works fine, but not at the initial refresh of the page when adding a new product but only after a hard refresh I see the button appear. Do I need to perform this check in a different lifecycle or gridview event so i can see the update immediately ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜