排序是未处理的,即使我有一个处理程序
本文关键字:有一个 处理 程序 未处理 排序 | 更新日期: 2023-09-27 17:57:50
我正在从SQL Server中提取数据,并将其显示在网格视图中。然后我试图通过点击标题来对列进行排序。我遇到了未处理事件的问题。。。。任何帮助都将是伟大的感谢
我遇到的问题是,当我点击一个标题来整理表格时,我会得到以下错误
一般错误
GridView"_propertyGridView"引发了未处理的事件Sorting。
来源错误:
在执行当前web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。
标记
<asp:GridView ID="_propertyGridView" runat="server" CssClass="table table-hover table-striped" AutoGenerateColumns="false" AllowSorting="true" GridLines="None" OnRowCommand="PropertyRowCommand" Width="100%">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Ref" SortExpression="Id" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="PostCode" HeaderText="Post Code" SortExpression="PostCode" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="ContractsFinishedOn" HeaderText="Contract Signed On" SortExpression="ContractsFinishedOn" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Mobile" HeaderText="Mobile No." SortExpression="Mobile" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="LandLordEmail" HeaderText="Owners Email" SortExpression="LandLordEmail" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="MoveInDate" HeaderText="Move In Date" SortExpression="MoveInDate" HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Status" SortExpression="Status1">
<ItemTemplate>
<asp:CheckBox ID="_tenantPaymentCheckBox" runat="server" Enabled="false" Checked='<%#Bind("TenantReceipt") %>' />
<asp:LinkButton ID="_tenantPaymentLink" Text="Send Tenant Receipt" CommandArgument='<%#Bind("Id") %>' CommandName="TenantEmail" runat="server" OnClientClick="javascript:return confirm('This will email & sms the tenant, please make sure its correct');" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" SortExpression="Status2">
<ItemTemplate>
<asp:CheckBox ID="_landlordInfoCheckBox" runat="server" Enabled="false" Checked='<%#Bind("LandlordInfo") %>' />
<asp:LinkButton ID="_landlordInfoLink" Text="Request Landlord Info" CommandArgument='<%#Bind("Id") %>' CommandName="LandlordInfoEmail" runat="server" OnClientClick="javascript:return confirm('This will email & sms the landlord, please make sure its correct');" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" SortExpression="Status3">
<ItemTemplate >
<asp:CheckBox ID="_landlordRentCheckBox" runat="server" Enabled="false" Checked='<%#Bind("LandlordReceipt") %>' />
<asp:LinkButton ID="_landlordRentLink" Text="Send Landlord Receipt" CommandArgument='<%#Bind("Id") %>' CommandName="LandlordEmail" runat="server" OnClientClick="javascript:return confirm('This will email & sms the landlord, please make sure its correct');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码隐藏
protected void _propertyGridView_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
string sortExpression = e.SortExpression;
ViewState["z_sortexpresion"] = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, "DESC");
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, "ASC");
}
}
catch (Exception ex)
{
SearchErrorLbl.Text = "error in such";
}
}
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set
{
ViewState["sortDirection"] = value;
}
}
private void SortGridView(string sortExpression, string direction)
{
DTSorting = new DataView(DTSorting, "", sortExpression + " " + direction, DataViewRowState.CurrentRows).ToTable();
_propertyGridView.DataSource = DTSorting;
_propertyGridView.DataBind();
}
public DataTable DTSorting
{
get
{
if (ViewState["Sorting"] != null)
return (DataTable)ViewState["Sorting"];
else
return null;
}
set
{
ViewState["Sorting"] = value;
}
}
您需要将_propertyGridView_Sorting事件附加到GridView。
<asp:GridView OnSorting="_propertyGridView_Sorting" ...>
...
</asp:GridView>
将OnSorting = "_propertyGridView_Sorting"
添加到网格视图标记中。
您在代码中定义了处理程序,但没有将其附加到网格。