我有一个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 Sort方法如下:
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
答案 0 :(得分:1)
单击“删除”按钮后,“页面加载”将发生在“删除”处理程序代码之前。因此,如果您绑定GridView
事件中的page_load
,则应绑定在!IsPostBack
条件下:
if (!IsPostBack)
{
GridView1.DataSource = MyDataSource;
GridView1.DataBind();
}
因为如果每次都绑定GridView
,数据将被重新加载,即GridView
重新填充未排序的数据,从而丢失先前的排序顺序。