【问题标题】:SortCommand Event handler of DataGrid not workingDataGrid 的 SortCommand 事件处理程序不起作用
【发布时间】:2010-05-27 10:10:41
【问题描述】:

我已经为 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>

handler的代码如下:

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

【问题讨论】:

  • 你给网格和处理程序同名了吗?这段代码看起来无法编译。
  • 网格 id -> Mygrid 处理程序 -> Mygrid_SortCommand
  • 代码编译和网格生成数据,唯一的问题是排序。排序处理程序根本不执行。
  • 不过,尝试使用更好的名称。这甚至可以解决它,但至少代码会更有意义。

标签: c# asp.net events webforms


【解决方案1】:

需要启用ViewState 进行排序。

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

【讨论】:

  • 是否启用了javascript?页面是否完全发回?
  • Java 脚本已启用,页面也在回发
  • 我正在尝试在 dtgBatches_SortCommand() 中设置一个断点。而且这个断点永远不会被击中。
  • 谢谢,这是真正的问题。我在启用视图状态后尝试过,现在一切似乎都正常了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多