2011-04-30 49 views
0

第1步: - 點擊更多 enter image description here如何使我的GridView中的鏈接單擊一下?

步驟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步..如何才能做到這一點?

如果我刪除更多的鏈接..它停止工作...請幫助我。

我必須點擊更多鏈接才能獲得添加到購物車的鏈接...我想添加一個項目到購物車中點擊一下..我現在有...我需要兩次點擊。

當我點擊更多它進入編輯模式..然後,當我點擊添加到購物車(這是在編輯模板)..它採取價格和提供編輯模板的值..如果我移動「添加到購物車」按鈕進入ItemTemplate ...它不能訪問報價和價格值。如果我將「添加到購物車」按鈕移動到ItemTemplate中,我將如何知道哪一行正在被點擊?

回答

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 
+0

no..actually我必須點擊更多鏈接以獲取添加到購物車的鏈接...我想只需點擊一下,即可將商品添加到購物車中。我現在擁有...我需要兩次點擊。 – Monodeep 2011-04-30 14:36:25

+0

當我點擊更多它去編輯模式..然後當我點擊添加到購物車(這是在編輯模板)..它需要價格和提供編輯模板的值..如果我移動「添加到購物車「按鈕進入ItemTemplate ...它不能訪問報價和價格值。 – Monodeep 2011-04-30 14:43:29

+0

如果我將「添加到購物車」按鈕移動到ItemTemplate中,我將如何知道哪一行被點擊? – Monodeep 2011-04-30 14:45:04

相關問題