我为DataGrid的OnSortCommand创建了一个事件处理程序:
<asp:DataGrid id="dtgBatches" runat="server" Width="100%" CssClass="intTable" EnableViewState="False" DataKeyField="bat_GUID"
GridLines="Horizontal" AllowSorting="True" AutoGenerateColumns="False" AllowPaging="False" >
<SelectedItemStyle BackColor="#FFFF99"></SelectedItemStyle>
<AlternatingItemStyle CssClass="intTableEntry"></AlternatingItemStyle>
<ItemStyle CssClass="intTableEntry2"></ItemStyle>
<HeaderStyle ForeColor="Black" CssClass="tableHeader"></HeaderStyle>
<Columns>
<asp:TemplateColumn >
<HeaderStyle Width="5%"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<img src="../images/icons/cog.png" onclick="universalAlert('Loading...',4,false); ViewBatch('<%# DataBinder.Eval(Container.DataItem, "bat_GUID") %>')" alt="view"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="bat_Name" SortExpression="bat_Name" HeaderText="<%$ Resources:AI360Resource, lnkbtn_Name %>">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Id" SortExpression="bat_Id" HeaderText="<%$ Resources:AI360Resource, ltxt_ID %>">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Date" SortExpression="bat_Date" HeaderText="<%$ Resources:AI360Resource, alt_date %>" DataFormatString="{0:d}">
<HeaderStyle HorizontalAlign="right" Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Right"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn>
<HeaderStyle Width="1%"></HeaderStyle>
</asp:TemplateColumn>
<asp:BoundColumn DataField="bat_Close_date" SortExpression="bat_Close_date" HeaderText="<%$ Resources:AI360Resource, ltxt_closed %>" DataFormatString="{0:d}">
<HeaderStyle Width="29%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Cont_Amount" SortExpression="bat_Cont_Amount" HeaderText="<%$ Resources:AI360Resource, alt_receipts %>" DataFormatString="{0:c}">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="bat_Disb_Amount" SortExpression="bat_Disb_Amount" HeaderText="<%$ Resources:AI360Resource, alt_disb %>" DataFormatString="{0:c}">
<HeaderStyle Width="25%"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
处理程序的代码如下:
protected void dtgBatches_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string strCurrentSort = dtgBatches.Attributes["SortExpr"];
string strNewSort = e.SortExpression;
if ((strCurrentSort != null) && (strCurrentSort == strNewSort))
{
//reverse direction
strNewSort += " DESC";
}
// Code to Set DataView dv
dv.Sort = strNewSort;
dtgBatches.DataSource = dv;
dtgBatches.DataBind();
}
问题是处理程序永远不会执行。
处理程序的注册如下:
private void InitializeComponent()
{
this.dtgBatches.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgBatches_SortCommand);
}