views:

50

answers:

1

I have created an event handler for the OnSortCommand of DataGrid:

<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>

And the code of handler is as below:

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();
        }

The problem is that the handler never executes.

The registartion for the handler is done as below:

private void InitializeComponent()
        {
            this.dtgBatches.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgBatches_SortCommand);
        }
+1  A: 

ViewState needs to be enabled for sorting.

http://msdn.microsoft.com/en-us/library/ms972427.aspx

David Neale
Still the same results.
Nadeem
Is javascript enabled? Is the page posting back at all?
David Neale
Java script is enabled and the page is also posting back
Nadeem
I am trying after putting a break point in dtgBatches_SortCommand().And this break point is never hit.
Nadeem
See edit. ViewState needs enabling.
David Neale
Thanks Man this was the real issue. I tried after enabling the view state and everything seems to be working now.
Nadeem