如何在我的GridView中单击链接?

时间:2011-04-30 14:30:20

标签: asp.net

第1步: - 点击更多 enter image description here

第2步: - 点击添加到购物车,将商品添加到购物车 enter image description here

这是我的gridview

<asp:GridView ID="GridView1" runat="server" 
                                                   AllowSorting="True" 
                                                   AutoGenerateColumns="False" 
                                                   CellPadding="4"      
                                                   ForeColor="#333333" 
                                                   GridLines="None" 
                                                   Width="810px" 
                                                   CssClass="gridview"                               
                                                   DataSourceID="SqlDataSource3" >
                      <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                      <Columns>
                      <asp:TemplateField ShowHeader="False">
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">More...</asp:LinkButton>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:LinkButton ID="GVAddToCart" runat="server" onclick="GVAddToCart_Click">Add to cart</asp:LinkButton>
                            </EditItemTemplate>
                      </asp:TemplateField>
                    <asp:TemplateField HeaderText="itemName" SortExpression="itemName">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("itemName") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="itemName" runat="server" Text='<%# Eval("itemName") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="price" SortExpression="price">
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("price") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="price" runat="server" Text='<%# Eval("price") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="offer" SortExpression="offer">
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("offer") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="offer" runat="server" Text='<%# Eval("offer") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="availability" SortExpression="availability">
                        <ItemTemplate>
                            <asp:Label ID="Label4" runat="server" Text='<%# Bind("availability") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="availability" runat="server" Text='<%# Eval("availability") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="shopName" SortExpression="shopName">
                        <ItemTemplate>
                            <asp:Label ID="Label5" runat="server" Text='<%# Bind("shopName") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="shopName" runat="server" Text='<%# Eval("shopName") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="address" SortExpression="address">
                        <ItemTemplate>
                            <asp:Label ID="Label6" runat="server" Text='<%# Bind("address") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="address" runat="server" Text='<%# Eval("address") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="email" SortExpression="email">
                        <ItemTemplate>
                            <asp:Label ID="Label7" runat="server" Text='<%# Bind("email") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="email" runat="server" Text='<%# Eval("email") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="phone" SortExpression="phone">
                        <ItemTemplate>
                            <asp:Label ID="Label8" runat="server" Text='<%# Bind("phone") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="phone" runat="server" Text='<%# Eval("phone") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    </Columns>
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                                           ConnectionString="<%$ ConnectionStrings:databaseConnectionString %>" 
                                           SelectCommand="SELECT shopList.itemName, shopList.offer, shopList.price, shopList.availability, shopDescription.shopName, shopDescription.address, shopDescription.email, shopDescription.phone FROM shopList INNER JOIN shopDescription ON shopList.shopID = shopDescription.shopID">
    </asp:SqlDataSource>

这是添加到购物车链接按钮的代码

Protected Sub GVAddToCart_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If User.Identity.Name = "" Then
            MsgBox("You need to login first before adding anything to the cart!", , "")
            Response.Redirect("~/login.aspx", True)
            Exit Sub
        End If

        Dim sCart = New cart
        Dim itemName, GVPrice, offer As Label
        Dim userID, buyNo, itemID As String

        itemID = Request.QueryString("itemID")
        itemName = CType(ListView1.Items(0).FindControl("itemName"), Label)
        userID = User.Identity.Name

        Dim index As Integer = GridView1.EditIndex
        GVPrice = CType(GridView1.Rows(index).FindControl("price"), Label)
        GVPrice.Text = CType(GVPrice.Text, Integer)
        offer = CType(GridView1.Rows(index).FindControl("offer"), Label)

        If sCart.CheckIfItemPresent(userID, itemID, GVPrice.Text, offer.Text) = True Then
            Exit Sub
        End If

        buyNo = sCart.findLatestBuyNo(userID)
        Session("buyNo") = buyNo

        Session("buyNo") = sCart.AddToCart(itemID, itemName.Text, GVPrice.Text, offer.Text, buyNo, userID)
    End Sub

所以我想要做的是删除步骤1 ..我可以这样做吗?

如果我删除更多链接..它停止工作......请帮助我。

我必须点击更多链接才能获得添加到购物车链接...我想在一次点击中将项目添加到购物车..我现在拥有的...我需要两次点击。< / p>

当我点击更多时,它进入编辑模式..然后当我点击添加到购物车(在编辑模板中)..它从编辑模板中获取价格和提供值..如果我移动“添加到购物车“按钮进入ItemTemplate ...它无法访问优惠和价格值。如果我将“添加到购物车”按钮移动到ItemTemplate,我将如何知道正在点击哪一行?

1 个答案:

答案 0 :(得分:1)

根据您的描述“删除步骤1”,听起来您希望链接/按钮字段始终显示为“添加到购物车”。

建议创建ButtonField而不是ItemTemplatesEditItemTemplates。看来你不需要编辑功能,而只需要一种方法来访问行上的那些属性。

 <asp:GridView OnRowCommand="GridView_RowCommand">

     <asp:ButtonField buttontype="Link" 
                      commandname="Add" 
                      text="Add to Cart"/>

      <asp:BoundField datafield="itemName" headertext="Item"/>
      <asp:BoundField datafield="itemPrice" headertext="Your Price"/>
      <asp:BoundField datafield="shopName" headertext="Shop Name"/>
      ..... etc.

你的代码隐藏需要像这样改变:

  Sub GridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

    If e.CommandName = "Add" Then      
      Dim index As Integer = Convert.ToInt32(e.CommandArgument)
      Dim row As GridViewRow = GridView1.Rows(index)

      Dim itemName As String = Server.HtmlDecode(row.Cells(1).Text) 
      Dim itemPrice As String = Server.HtmlDecode(row.Cells(2).Text) 
      '...... etc
      'All required business logic to add to cart.
    End If

  End Sub