Click here to Skip to main content
15,885,998 members

add click event to an imagebutton generated in listView

aspCoder2012 asked:

Open original thread
hi all, am quite new to asp.net am trying to implement an 'add to cart' button which is generated in a listView
on clicking the imagebutton am trying to add the the product id and quantity=1 to my sortedlist Session
i tried to use command argument but no success
am i doing it right ?

ASP.NET
<asp:ListView  ID="listview1" GroupItemCount="3" runat="server">
        <LayoutTemplate>
            <div id="itemListView">
                <asp:PlaceHolder runat="server" ID="groupPlaceHolder" />
            </div>
        </LayoutTemplate>
        <GroupTemplate>
            <div style="clear: both;">
                <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
            </div>
        </GroupTemplate>
        <ItemTemplate>
            <div class="lvProduct">
                
               
                <div class="productDisplayImage">
                        <asp:Imagebutton  imageURl='<%# Eval("imageUrl") %>'
                       PostBackUrl= '<%#string.concat("~/productDetail.aspx?pid=",Eval("pid")) %>'
                       
                        width="175px" Height="160px" runat="server"/>
                        
                </div>
                
                <div class="productPriceDisplay">
                        Price: $<%# Eval("price")%> <asp:ImageButton imageUrl="~/assignment/images/addToCartButton.png" ImageAlign="top" 
 onCommand="addToCart" commandArgument= '<%#string.concat("Eval("pid")) %>'                        
                        Width="100px" height="25px" style="margin-left:10px;" runat="server"/>
                </div>
            </div>
        </ItemTemplate>
        <Itemseparatortemplate>
            <div class="itemSep">
            </div>
        </ItemSeparatorTemplate>
        <GroupSeparatorTemplate>
            <div class="groupSep">
            </div>
        </GroupSeparatorTemplate>
        <EmptyDataTemplate>
        </EmptyDataTemplate>
    </asp:ListView>


VB
Protected Function addToCart(ByVal sender As Object, ByVal e As CommandEventArgs) As Integer



        Dim pid As String = e.CommandArgument.ToString


        If Not IsNothing(Session("cart")) Then
            Dim slCart As SortedList = CType(Session("cart"), SortedList)
            slCart.Add(pid, "1")
            Response.Redirect("~/testing.aspx")
        Else
            Dim slCart As New SortedList

            slCart.Add(pid, "1")
 Response.Redirect("~/testing.aspx")

        End If

        Return Nothing
    End Function
Tags: Visual Basic, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900