การเรียงลำดับ GridView ด้วย ImageButtons และคำสั่ง Delete ไม่ทำงาน

ฉันมีตัวควบคุม GridView ซึ่งมี 5 ฟิลด์ที่ถูกผูกไว้ซึ่งเปิดใช้งานการเรียงลำดับและ 4 ฟิลด์เทมเพลต ช่องเทมเพลตช่องหนึ่งคือปุ่มลบรูปภาพที่จะลบแถวเมื่อคลิก ตอนนี้ทุกอย่างเป็นไปด้วยดีเมื่อผู้ใช้เพียงแค่ใช้ gridview เหมือนเดิมโดยไม่ต้องเรียงลำดับ แต่เมื่อพวกเขาเรียงลำดับแล้วกดลบ อาร์กิวเมนต์คำสั่งจะได้รับข้อมูลแถวที่ไม่ถูกต้องและลบออกแทนที่จะลบสิ่งที่พวกเขาเลือกที่จะลบ สิ่งนี้เกิดขึ้นเฉพาะกับฟิลด์เทมเพลต 2 ช่องที่มีปุ่มควบคุมรูปภาพ

<Columns>
        <asp:BoundField DataField="AccountNo" HeaderText="Account No" 
            SortExpression="AccountNo" />
        <asp:BoundField DataField="Address" HeaderText="Address" 
            SortExpression="Address" />
        <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
        <asp:BoundField DataField="Name" HeaderText="Name" 
            SortExpression="Name" />
        <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
        <asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
        <asp:BoundField DataField="Utility" HeaderText="Utility" 
            SortExpression="Utility" />                
        <asp:TemplateField HeaderText="Edit">
            <ItemTemplate>
                <asp:HyperLink runat="server" ID="EditLink"  ToolTip="Edit Account" NavigateUrl='<%# GetEditURL(((BillingEntity)Container.DataItem).Id) %>' >
                    <img src="../img/edit.png" border="0"/>
                </asp:HyperLink>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center"></ItemStyle>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Enable/Disable" >
            <ItemTemplate>
                <asp:ImageButton runat="server" ID="DisableButton" ImageUrl = "../img/delete.png" ToolTip="Disable Account" CommandName="Disable_Account" CommandArgument='<%#((BillingEntity)Container.DataItem).Id %>' OnClientClick="if (confirm('Are you sure you want to disable this account?')==false) {return false;}" Visible='<%# ShowDisableButton(((BillingEntity)Container.DataItem).Status)%>'/>
                <asp:ImageButton runat="server" ID="EnableButton" ImageUrl = "../img/add.png" ToolTip="Enable Account" CommandName="Enable_Account" CommandArgument='<%#((BillingEntity)Container.DataItem).Id %>'   Visible='<%# ShowEnableButton(((BillingEntity)Container.DataItem).Status)%>'/>                    
            </ItemTemplate>                
            <ItemStyle HorizontalAlign="Center"></ItemStyle>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Delete" >
            <ItemTemplate>
                <asp:ImageButton runat="server" ID="Delete" ImageUrl = "../img/cross.png" ToolTip="Delete Account" CommandName="Delete_Account" CommandArgument='<%#((BillingEntity)Container.DataItem).Id %>' OnClientClick="if (confirm('Invoices associated with this account will be deleted permanently. Are you sure you want to delete this account?')==false) {return false;}" />
            </ItemTemplate>                
            <ItemStyle HorizontalAlign="Center"></ItemStyle>
        </asp:TemplateField>            
       <asp:TemplateField HeaderText="View Invoices" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:HyperLink runat="server" ID="ViewInvoiceLink" ToolTip="Recent invoices" NavigateUrl='<%# GetViewInvoiceURL(((BillingEntity)Container.DataItem).Id) %>' >
                    <img src="../img/go.png" border="0"/>
                </asp:HyperLink>
            </ItemTemplate>

            <ItemStyle HorizontalAlign="Center"></ItemStyle>
        </asp:TemplateField>            
       <asp:TemplateField HeaderText="Submit Invoice" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:HyperLink runat="server" ID="InvoiceLink"  ToolTip="Submit invoice" NavigateUrl='<%# GetSubmitInvoiceURL(((BillingEntity)Container.DataItem).Id) %>' >
                    <img src="../img/go.png" border="0"/>
                </asp:HyperLink>
            </ItemTemplate>

            <ItemStyle HorizontalAlign="Center"></ItemStyle>
        </asp:TemplateField>
</Columns>

แก้ไข-โค้ดแหล่งข้อมูล

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    SelectMethod="GetAllEntities" 
    TypeName="DataAccessLayer.Repository.BillingEntityRepository">
    <SelectParameters>
        <asp:QueryStringParameter DefaultValue="-1" Name="clientId" 
            QueryStringField="clientId" Type="Int32" />
        <asp:Parameter DefaultValue="Name" Name="sortColumn" Type="String" />
        <asp:Parameter DefaultValue="ASC" Name="sortOrder" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

และวิธีการจัดเรียง GridView ของฉันเป็นดังนี้:

protected void GridView_BillingEntity_Sorting(object sender, GridViewSortEventArgs e)
{
    if (ObjectDataSource1.SelectParameters.Count == 3)
    {
        ObjectDataSource1.SelectParameters[1].DefaultValue = e.SortExpression.ToString();
        ObjectDataSource1.SelectParameters[2].DefaultValue = GetSortDirection(e.SortExpression);

        GridView_BillingEntity.DataBind();

        e.Cancel = true;
    }
}

แก้ไข-แถวตามที่ผู้ใช้เห็น

นี่คือวิธีที่ผู้ใช้เห็นแถวบนเบราว์เซอร์ การคลิกปุ่มลบจะเป็นการลบแถวและรีเฟรชตาราง


person Sai    schedule 03.04.2014    source แหล่งที่มา
comment
ในเหตุการณ์ _Sorting คุณผูก Gridview กับอะไร ฉันไม่เห็นการตั้งค่าแหล่งข้อมูล   -  person Martin Smellworse    schedule 03.04.2014
comment
@MartinSmellworse: ฉันได้แก้ไขโพสต์ของฉันเพื่อรวม DataSource   -  person Sai    schedule 03.04.2014
comment
คุณบอกว่าสิ่งที่พวกเขาเลือกที่จะลบ? พวกเขาเลือกแถวอย่างไร? มีปุ่มเลือกหรือวิธีอื่นไหม?   -  person R.C    schedule 04.04.2014
comment
@FlopScientist: เพิ่มรูปภาพเพื่อแสดงวิธีที่ผู้ใช้สามารถเลือกลบแถวได้ ในตัวอย่างนี้ ฉันมีหนึ่งแถว เมื่อมีหลายแถวและฉันจัดเรียงตามหนึ่งใน 6 คอลัมน์แรกแล้วกดลบในแถว แถวสุ่มที่แตกต่างจากแถวที่ฉันเลือกจะถูกลบออก   -  person Sai    schedule 04.04.2014
comment
จะเกิดอะไรขึ้นหากคุณแสดงอาร์กิวเมนต์คำสั่งของ ImageButton ที่จะลบป้ายกำกับในเซลล์เดียวกัน มันเก็บค่าที่ถูกต้องเมื่อคุณเรียงลำดับหรือไม่? ฉันไม่เคยเห็น GridView ผูกมัดเหมือนที่คุณทำมาแล้ว ฉันมักจะใช้ชุดข้อมูล - เก็บไว้ใน ViewState หากมันไม่ใหญ่เกินไปและเปลี่ยน SortOrder ของชุดข้อมูลก่อนที่จะรวมข้อมูลในแต่ละประเภท   -  person Martin Smellworse    schedule 04.04.2014
comment
อาจมีข้อผิดพลาดเล็กน้อยในโค้ดที่คุณตรวจสอบ CommandName เพื่อดำเนินการที่เหมาะสม เช่น: Delete_Account   -  person R.C    schedule 04.04.2014
comment
@MartinSmellworse: ใช่ การเรียงลำดับที่น่าสนใจยังคงรักษา CommandArgument Value แต่เมื่อฉันดีบั๊กผ่านโค้ดตัวจัดการหลังจากกดลบ อาร์กิวเมนต์คำสั่งที่ถูกส่งไปไม่ใช่สิ่งที่อยู่ในป้ายกำกับ!   -  person Sai    schedule 04.04.2014
comment
ให้แม่นยำ มันเหมือนกับค่าอาร์กิวเมนต์คำสั่งที่อยู่ในแถวนั้นก่อนที่จะเรียงลำดับ   -  person Sai    schedule 04.04.2014
comment
ฟังดูเหมือนคุณอาจผูกกริดสองครั้ง ทุกครั้งที่คุณ postback และอีกครั้งเมื่อคุณเรียงลำดับ หากการลบของคุณเกิดขึ้นระหว่างสอง databinds มันจะอธิบายว่าทำไมจึงเลือก ID 'สุดท้าย' แก้ไข. ฉันเห็นคนอื่นได้ให้คำแนะนำแบบเดียวกันนี้แล้ว   -  person Martin Smellworse    schedule 04.04.2014


คำตอบ (1)


เมื่อคลิกปุ่ม Delete การโหลดเพจจะเกิดขึ้นก่อนโค้ดตัวจัดการ Delete ของคุณ ดังนั้น หากคุณผูก GridView ในเหตุการณ์ page_load คุณควรผูกภายใต้เงื่อนไข !IsPostBack:

if (!IsPostBack)
{
    GridView1.DataSource = MyDataSource;
    GridView1.DataBind();
}

เพราะหากคุณผูก GridView ทุกครั้ง ข้อมูลจะถูกโหลดใหม่ กล่าวคือ GridView จะถูกเติมข้อมูลซ้ำด้วยข้อมูลที่ไม่ได้เรียงลำดับ ซึ่งจะทำให้ลำดับการจัดเรียงก่อนหน้าหายไป

person R.C    schedule 03.04.2014