OnSelectedIndexChanged runs twice!

V

VB Programmer

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!
 
V

VB Programmer

FYI: Even AFTER I comment out the BindShoppingCart call it still calls
RemoveCartItem twice...

Any ideas?
 
V

VB Programmer

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

Patrick.O.Ige

hmm Good you noticed that
Patrick

VB Programmer said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top