We help IT Professionals succeed at work.
Get Started

GridView RowUpdating event fires twice

4,455 Views
Last Modified: 2012-08-21
I have a GridView that, for some odd reason, seams to fire twice with I click the "Update" linkbutton.  I viewed this when debugging the page.  The first time through it perfroms the update, then it tries a second time to do the update again, except the 2nd time around there are no form values to pass so it errors out.

Here's my GridView Code Behind code...

    Public Sub BindItemsGrid()
        Dim dt As DataTable = itemsLogic.GetPickTicketItems(hfPickTicketID.Value)
        If dt.Rows.Count > 0 Then
            ItemsGrid.DataSource = dt
            ItemsGrid.DataBind()
        End If
    End Sub

    Protected Sub ItemsGrid_RowCancelingEdit(sender As Object, e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles ItemsGrid.RowCancelingEdit
        ItemsGrid.EditIndex = -1
        BindItemsGrid()
    End Sub

    Protected Sub ItemsGrid_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles ItemsGrid.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If ItemsGrid.EditIndex = e.Row.RowIndex Then
                Dim ddOpp As DropDownList = DirectCast(e.Row.Cells(3).FindControl("DropDownList1"), DropDownList)

                '  Bind the Bin Loc to the drop down list
                ' -------------------------------------------------------------------------------
                Dim MyBLL As New PickPackBLL()
                Dim dt As DataTable = MyBLL.GetBinLocs()

                ddOpp.DataSource = dt
                ddOpp.DataTextField = "BinLoc"
                ddOpp.DataValueField = "BinLoc"
                ddOpp.DataBind()

                '  Set the Selected Value of the drop down list.
                ' -------------------------------------------------------------------------------
                Dim SelectedBinLoc As String
                Dim defaultSelectedIndex As String = "Unassigned"

                Dim pickTicketLogic As New PickPackItemsBLL()
                Dim dtpick As DataTable = pickTicketLogic.GetPickTicketItems(hfPickTicketID.Value)

                If dtpick.Rows.Count > 0 Then
                    Dim row As DataRow = dtpick.Rows(0)
                    SelectedBinLoc = row("BinLoc")
                End If

                If ddOpp.Items.FindByValue(SelectedBinLoc) IsNot Nothing Then
                    ddOpp.SelectedValue = SelectedBinLoc
                Else
                    ddOpp.SelectedValue = defaultSelectedIndex
                End If

            End If
        End If
    End Sub

    Protected Sub ItemsGrid_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles ItemsGrid.RowDeleting
        Dim Id As Integer = Convert.ToInt32(ItemsGrid.DataKeys(e.RowIndex).Values(0).ToString())
        itemsLogic.DeletePickTicketItem(Id)
        Call BindItemsGrid()
    End Sub

    Protected Sub ItemsGrid_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles ItemsGrid.RowEditing
        ItemsGrid.EditIndex = e.NewEditIndex
        Call BindItemsGrid()
    End Sub

    Protected Sub ItemsGrid_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles ItemsGrid.RowUpdating
        Dim ItemID As Integer = CInt(ItemsGrid.DataKeys(e.RowIndex).Value.ToString())
        Dim ItemQuantity As TextBox = DirectCast(ItemsGrid.Rows(e.RowIndex).FindControl("tb_ItemQty"), TextBox)
        Dim ItemPart As TextBox = DirectCast(ItemsGrid.Rows(e.RowIndex).FindControl("tb_ItemPart"), TextBox)
        Dim ItemBinLoc As DropDownList = DirectCast(ItemsGrid.Rows(e.RowIndex).FindControl("DropDownList1"), DropDownList)
        Dim ItemShelf As DropDownList = DirectCast(ItemsGrid.Rows(e.RowIndex).FindControl("DropDownList2"), DropDownList)

        itemsLogic.UpdatePickTicketItem(ItemID, ItemQuantity.Text, ItemPart.Text, ItemShelf.SelectedValue, ItemBinLoc.SelectedValue)

        ItemsGrid.EditIndex = -1
    End Sub

Open in new window


And... if you need it... here's my GridView code:

<asp:GridView ID="ItemsGrid" runat="server" 
                    AutoGenerateColumns="False"
                    DataKeyNames="ItemID" 
                    OnRowCancelingEdit="ItemsGrid_RowCancelingEdit"
                    OnRowDataBound="ItemsGrid_RowDataBound" 
                    OnRowEditing="ItemsGrid_RowEditing"
                    OnRowUpdating="ItemsGrid_RowUpdating" 
                    OnRowDeleting="ItemsGrid_RowDeleting"                      
                    Width="100%" 
                    CssClass="mGrid" 
                    BorderStyle="None" 
                    GridLines="None">
                    <Columns>
                        <asp:TemplateField HeaderText="Options" ShowHeader="False">
                            <EditItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                                    CommandName="Update" Text="Update" ValidationGroup="vsItems"></asp:LinkButton>
                                &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                                    CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                                    CommandName="Edit" Text="Edit"></asp:LinkButton>
                                &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                                    CommandName="Delete" Text="Delete" OnClientClick='return confirm("Are you sure you want to delete this item?");'></asp:LinkButton>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Left" Width="80px" />
                            <ItemStyle VerticalAlign="Top" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="QTY" SortExpression="Quantity">
                            <ItemStyle VerticalAlign="Top" />
                            <EditItemTemplate>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please enter a quantity for your item." ControlToValidate="tb_ItemQty" Display="Dynamic" ValidationGroup="vsItems" Text="Err"></asp:RequiredFieldValidator>
                                <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Incorrect value for QTY" ControlToValidate="tb_ItemQty" Operator="DataTypeCheck" Type="Integer" Display="Dynamic" ValidationGroup="vsItems" Text="Err"></asp:CompareValidator>
                                <asp:TextBox ID="tb_ItemQty" runat="server" CssClass="generaltext_A2A" 
                                    Height="22px" Text='<%# Bind("Quantity") %>' Width="18px"></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Part" SortExpression="Part">
                            <ItemStyle VerticalAlign="Top" />
                            <EditItemTemplate>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter Part information." ControlToValidate="tb_ItemPart" Display="Dynamic" ValidationGroup="vsItems" Text="Err"></asp:RequiredFieldValidator>
                                <asp:TextBox ID="tb_ItemPart" runat="server" CssClass="generaltext_A2A" 
                                    Height="80px" Text='<%# Bind("Part") %>' TextMode="MultiLine" Width="180px" MaxLength="480"></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("Part") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="BinLoc" SortExpression="Bin">
                            <ItemStyle VerticalAlign="Top" />
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" CssClass="generaltext_A2A">
                                </asp:DropDownList>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("BinLoc") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Shelf" SortExpression="Shelf">
                            <ItemStyle VerticalAlign="Top" />
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList2" runat="server" CssClass="generaltext_A2A" 
                                    SelectedValue='<%# Bind("Shelf") %>'>
                                    <asp:ListItem>1</asp:ListItem>
                                    <asp:ListItem Value="2"></asp:ListItem>
                                    <asp:ListItem Value="3"></asp:ListItem>
                                    <asp:ListItem Value="4"></asp:ListItem>
                                    <asp:ListItem Value="5"></asp:ListItem>
                                </asp:DropDownList>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Shelf") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

Open in new window


Any idea as to what I might be doing incorrectly?
Comment
Watch Question
Practice Manager
Commented:
This problem has been solved!
Unlock 2 Answers and 3 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE