开发者

How can I make the link in my GridView a single click?

开发者 https://www.devze.com 2023-03-01 14:04 出处:网络
Step 1:- Click on more Step 2:- Click on add to cart to add an item to cart This is my gridview <asp:GridView ID=\"GridView1\" runat=\"server\"

Step 1:- Click on more

How can I make the link in my GridView a single click?

Step 2:- Click on add to cart to add an item to cart

How can I make the link in my GridView a single click?

This is my 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>

This is the code behind for the add to cart link button

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

So what I want to do is to remove the Step 1..how can i do this?

If i remove the more link..it stops working...please help me.

I have to click on the more link to to get the Add to cart link...I want to add an item to the cart in one click..with what i have now ...I need two clicks.

When I click on more it goes to edit mode..then when I click on add to cart(which is in edit template)..it takes the price and offer values from the edit template..if i move the "Add To Cart" button into the ItemTemplate...it cant access the offer and price values. If i move the "Add To Cart" button into the ItemTemplate how will I know which row is being clicked?


By your description "Remove The Step 1", it sounds like you want that link/button field shown as Add To Cart all the time.

Suggest creating a ButtonField instead of your ItemTemplates and EditItemTemplates. It appears you don't need editing capability, but rather just a way to access those properties on the row.

 <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.

Your code-behind would need to be changed like so:

  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

精彩评论

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