将click事件添加到listView中生成的imagebutton中 [英] add click event to an imagebutton generated in listView
本文介绍了将click事件添加到listView中生成的imagebutton中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,对于asp.net来说是一个很新的东西,我试图实现一个在listView中生成的添加到购物车"按钮.
单击imagebutton时,试图将产品ID和数量= 1添加到我的sortedlist会话中
我尝试使用命令参数但没有成功
我做对了吗?
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: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>
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
推荐答案
<%#Eval(" )%> < asp:ImageButton imageUrl =" ImageAlign =" 顶部" onCommand =" addToCart" commandArgument " Eval("</span> pid ))%>' 宽度=" 100px height =" 25px " margin-left:10px; " server " itemSep > </div> </ItemSeparatorTemplate> < GroupSeparatorTemplate> < div class =" groupSep > </div> </GroupSeparatorTemplate> < EmptyDataTemplate> </EmptyDataTemplate> </asp:ListView>
<%# 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>
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
您遇到的问题是因为触发addCart方法时,会话变量"cart"为空/为空.我已更新您的代码,如下所示:
The issue you are having is because the session variable "cart" is nothing/null when the addCart method is fired. I have updated your code as below:
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")
Session("cart") = slCart '<-- Missing this line
Response.Redirect("~/testing.aspx")
End If
Return Nothing
End Function
为了进一步增强页面逻辑的机制并遵循良好的编码习惯,请将会话购物车变量定义为类成员:
To further enhance the mechanics of your page logic and to work with good coding practice, is to define your session cart variable as a class member:
Private Property cart() As SortedList
Get
If Session("cart") Is Nothing Then
Session("cart") = New SortedList
End If
Return Session("cart")
End Get
Set(ByVal value As SortedList)
Session("cart") = value
End Set
End Property
这篇关于将click事件添加到listView中生成的imagebutton中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文