7 Replies - 2455 Views - Last Post: 01 August 2010 - 08:53 AM Rate Topic: -----

#1 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

sqldatasource posting back twice..

Posted 28 July 2010 - 05:22 AM

I am using an sqldatasource on a grid, with a delete command. If the user adds two Names accidentally, they can simply remove one of the duplicates. I achieved this by using


DeleteCommand="DELETE TOP(1) FROM [Data] WHERE [Name] = @Name"

...





and


<asp:CommandField ButtonType="Image" DeleteImageUrl="/delbutton2.jpg" ShowDeleteButton="True" />





If I run this sql query manually in sql management studio, it deletes one of the doubly added names (correct). But in the sqldatasource it still deletes both of them, even though I've clicked delete once.


It appears it is posting back twice, but Im not sure why.. my pageload doesnt call it or anything

This post has been edited by irishgirl: 28 July 2010 - 06:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: sqldatasource posting back twice..

#2 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: sqldatasource posting back twice..

Posted 28 July 2010 - 05:51 AM

A query trace tells me it's executing from the sqldatasource twice..but why :s
Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: sqldatasource posting back twice..

Posted 28 July 2010 - 07:31 AM

You are going to need to post more of your code in order to determine what may be causing the issue.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: sqldatasource posting back twice..

Posted 28 July 2010 - 09:54 AM

post your Page_Load method
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: sqldatasource posting back twice..

Posted 28 July 2010 - 09:58 AM

It sounds as if you're calling it twice (without knowing it) so as eclipsed4utoo suggested show the code in your load event
Was This Post Helpful? 0
  • +
  • -

#6 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: sqldatasource posting back twice..

Posted 29 July 2010 - 05:36 AM

Guys, I did some research and apparently GridView RowDeleting event fires twice when you use an imagebutton. Here is a fix I found

Quote

Currently when you build a command field for a delete button it will look something like this.

     <asp:CommandField ButtonType="Image" DeleteImageUrl="images/delete.gif" ShowDeleteButton="true"  />



By Changing the ButtonType to "Link" and modifying the DeleteText you will have the same delete image that works exactly like the Image Button Type but without the double firing event. Here is the modified code.

    
<asp:CommandField ButtonType="Link" DeleteText="<img src='images/delete.gif' alt='Delete this' border='0' />" ShowDeleteButton="true" />


Was This Post Helpful? 1
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: sqldatasource posting back twice..

Posted 29 July 2010 - 07:28 AM

Why are you handling the deletion of the row in the RowDeleting event?

This event is best used to provide a last minute means to cancel the deletion.

MSDN:

Quote

The RowDeleting event is raised when a row's Delete button is clicked, but before the GridView control deletes the row. This enables you to provide an event-handling method that performs a custom routine, such as canceling the delete operation, whenever this event occurs.


You should be handling the deletion of the row in the RowCommand event.

Here is an example:

Source:
                                    <asp:GridView ID="gvwPaymentAccts" runat="server" AutoGenerateColumns="False" Width="600px"
                                        OnRowDataBound="gvwPaymentAccts_RowDataBound" CellPadding="5" OnRowCommand="gvwPaymentAccts_RowCommand"
                                        OnRowDeleting="gvwPaymentAccts_RowDeleting" onselectedIndexChanged="gvwPaymentAccts_SelectedIndexChanged" EmptyDataText="Currently you do not have any payment accounts. If you would like to setup a new payment account then click the 'Add New Account' button.">
                                        <Columns>
                                            <asp:CommandField ButtonType="Button" SelectText="Update" ShowSelectButton="True">
                                                <ControlStyle CssClass="SubmitButtonS" />
                                            </asp:CommandField>
                                            <asp:TemplateField ShowHeader="False">
                                                <ItemTemplate>
                                                    <asp:Button ID="btnDeleteAccount" runat="server" CausesValidation="False" Text="Delete" />
                                                    <asp:Panel ID="pnlConfirmAccountDelete" runat="server" CssClass="modalPopUp" Style="display: none;
                                                        padding: 15px" Width="250px">
                                                        <center>
                                                            <asp:Label ID="lblRecurringPayment" runat="server" Font-Bold="true" Font-Size="small" ForeColor="red" Visible="false">
                                                                Warning: The payment account you are about to delete is currently being used for an Automatic Payment. If you delete this
                                                                payment account, then you will cancel any further automatic payments that use this payment account. 
                                                            </asp:Label><br /><br />
                                                            <asp:HiddenField ID="hfBillingAcct" runat="server" />
                                                            <asp:Label ID="lblNoStatement" runat="server" Font-Bold="True" Font-Size="Small"
                                                                ForeColor="Black">Are you sure you want to delete this payment account?</asp:Label>
                                                            <br /><br />
                                                            <center>
                                                                <font id="permMsg" runat="server" color="Red">(This change is permanent and cannot be
                                                                    undone)</font></center>
                                                            <br />
                                                            <asp:Button ID="btnDeleteOk" runat="server" CommandName="Delete" CommandArgument="<%# Container.DataItemIndex %>" CssClass="SubmitButtonS"
                                                                onclick="btnDeleteOk_Click" Text="Ok" Width="65px" />
                                                            <asp:Button ID="btnCancelDelete" runat="server" CssClass="SubmitButtonS" Text="Cancel"
                                                                Width="65px" />
                                                        </center>
                                                    </asp:Panel>
                                                    <ajaxToolkit:ModalPopupExtender ID="modalPopUp" runat="server" BackgroundCssClass="modalBackground"
                                                        CancelControlID="btnCancelDelete" DropShadow="true" PopupControlID="pnlConfirmAccountDelete"
                                                        TargetControlID="btnDeleteAccount">
                                                    </ajaxToolkit:ModalPopupExtender>
                                                </ItemTemplate>
                                                <ControlStyle CssClass="SubmitButtonS" />
                                            </asp:TemplateField>
                                            <asp:BoundField HeaderText="Account Nickname" DataField="PRNNIU" >
                                                <ItemStyle HorizontalAlign="center" />
                                            </asp:BoundField>
                                            <asp:BoundField HeaderText="Account Holder Name" DataField="PRNHNU" >
                                                <ItemStyle HorizontalAlign="center" />
                                            </asp:BoundField>
                                            <asp:BoundField HeaderText="Account Description" DataField="PRNAOU" >
                                                <ItemStyle HorizontalAlign="center" />
                                            </asp:BoundField>
                                            <asp:BoundField HeaderText="Account Number" DataField="PRNACU" >
                                                <ItemStyle HorizontalAlign="center" />
                                            </asp:BoundField>
                                        </Columns>
                                        <HeaderStyle BackColor="#001664" Font-Bold="True" ForeColor="White" Height="20px"
                                            VerticalAlign="Middle" />
                                        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                                        <RowStyle BackColor="White" ForeColor="Black" />
                                        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                                        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                                        <AlternatingRowStyle BackColor="#EEEEEE" />
                                        <EmptyDataRowStyle Font-Size="Medium" ForeColor="Red" />
                                    </asp:GridView>


Code-behind:
    protected void gvwPaymentAccts_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Select"))
        {
            btnUpdateACHAccount.Visible = true;
            btnUpdateCCD.Visible = true;
            DisableEnableGrid(true);
        }
        else if (e.CommandName.Equals("Delete"))
        {
            PaymentProcessor pp = new PaymentProcessor();
            UserSession userSess = (UserSession)Session["UserSess"];

            int gvwRowIndex = Convert.ToInt32(e.CommandArgument);
            GridViewRow gvrPaymentAcct = (GridViewRow)gvwPaymentAccts.Rows[gvwRowIndex];
            HiddenField hfBillingAcct = (HiddenField)gvrPaymentAcct.Cells[1].FindControl("hfBillingAcct");
            int deletePaymentKey;

            //Check if this has any recurring payments scheduled
            //If so, then delete the recurring payment
            if (hfBillingAcct.Value.Contains("~"))
            {
                string[] values = hfBillingAcct.Value.Split('~');
                deletePaymentKey = Convert.ToInt32(values[0]);
                pp.DeleteRecurringPayment(userSess.UID, values[1]);
                Session.Remove("AccountNumbers");
            }
            else
                deletePaymentKey = Convert.ToInt32(hfBillingAcct.Value);

            //Delete the payment account
            pp.DeletePaymentAccount(userSess.UID, deletePaymentKey);
            BindAccounts(userSess);
        }
    }

Was This Post Helpful? 2
  • +
  • -

#8 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: sqldatasource posting back twice..

Posted 01 August 2010 - 08:53 AM

thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1