Thursday, March 29, 2012

OnSelectedIndexChanged runs twice!

Using ASP.NET 2.0...
My GridView is partially defined as such...
<asp:GridView ID="CartGrid" AutoGenerateColumns="False"
DataKeyNames="ID" OnSelectedIndexChanged="RemoveCartItem"
CellPadding="4" runat="Server"
ForeColor="Gray" GridLines="None" Width="90%"
BorderStyle="Inset"
BorderWidth="1px" Caption="Shopping Cart" Font-Bold="False"
CaptionAlign="Left">
<Columns>
<asp:ButtonField
CommandName="select" Text="Remove" />
<asp:BoundField
DataField="Name" HeaderText="Name">
<ControlStyle
Width="300px" />
<ItemStyle Width="300px"
/>
As you can see, when the user clicks "Remove" it runs the "RemoveCartItem"
function, defined as such...
Sub RemoveCartItem(ByVal s As Object, ByVal e As EventArgs) Handles
CartGrid.SelectedIndexChanged
Dim ID As String = CartGrid.SelectedDataKey.Value
Profile.MyShoppingCart.RemoveItem(ID)
BindShoppingCart()
End Sub
Sub BindShoppingCart()
If Not Profile.MyShoppingCart Is Nothing Then
CartGrid.DataSource = Profile.MyShoppingCart.CartItems
CartGrid.DataBind()
lblTotal.Text = Profile.MyShoppingCart.Total.ToString("c")
BindShippingDdl()
End If
End Sub
I put a breakpoint on RemoveCartItem and the code is running twice each
time. Any ideas why?
Thanks!hmm Good you noticed that
Patrick
"VB Programmer" <dont@.emailme.com> wrote in message
news:u3vqAAhTGHA.4132@.TK2MSFTNGP11.phx.gbl...
> Got it! I removed "Handles CartGrid.SelectedIndexChanged" from
> RemoveCartItem and it works. It was calling twice, once from the dg and
> once from the SelectedIndexChanged event I suppose.
> "VB Programmer" <dont@.emailme.com> wrote in message
> news:ufz0LcgTGHA.4264@.TK2MSFTNGP11.phx.gbl...
>

0 comments:

Post a Comment