ASP.NET Gridview Linkbutton隐藏/显示页面加载问题

时间:2011-08-05 11:31:32

标签: asp.net

我试图隐藏/显示我的gridview中的链接按钮,而不是Gridview中的行是我购物车的一部分。

这是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>

我此刻尝试这样做的方法是按行数据绑定

     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

它工作正常,但在添加新产品时不会在页面的初始刷新时,但仅在硬刷新后才会看到按钮出现。我是否需要在不同的生命周期或gridview事件中执行此检查,以便我可以立即看到更新?

0 个答案:

没有答案