Reputation: 712
I have a GridView control which has 5 bound fields that are sort enabled and 4 template fields. One of the template fields is the Delete Image button that deletes the row when clicked. Now, all is well when users just used the gridview as it is, without sorting. But when they sort it and then press delete, the command argument receives the wrong row information and deletes it instead of deleting what they chose to delete. This happens only with the 2 template fields that have an image button control.
<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>
EDIT-Data Source Code
<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>
And my GridView Sort method is as follows:
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;
}
}
EDIT-Rows as seen by the user
Upvotes: 1
Views: 517
Reputation: 10565
On clicking the Delete button, Page Load occurs before your Delete handler code. So if you bind the GridView
in the page_load
event, You should bind under a !IsPostBack
condition:
if (!IsPostBack)
{
GridView1.DataSource = MyDataSource;
GridView1.DataBind();
}
Because if you bind the GridView
every time , data will be loaded afresh, i.e the GridView
is repopulated with unsorted data, thereby losing the previous sort order.
Upvotes: 1