Gridview 事件订阅

时间:2021-01-22 16:07:45

标签: c# asp.net gridview webforms

我一直在浏览几篇文章,试图找出我的 gridview 中有什么不正确的地方。我正在尝试允许排序。 我已确保 EnableSortingAndPagingCallbacks 设置正确

EnableSortingAndPagingCallbacks="false" 

还有这些

AllowSorting="true" OnSorting="gvEntryPivot_Sorting"

我在页面中的这个元素之前有这个

<asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>

我的 HTML 代码

            <asp:UpdatePanel ID="panelGridView" runat="server" UpdateMode="Conditional" ClientIDMode="Static">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnSummary" EventName="Command" />
                    <asp:AsyncPostBackTrigger ControlID="btnEntry" EventName="Command" />
                    <asp:AsyncPostBackTrigger ControlID="btnOverdue" EventName="Command" />
                    <asp:AsyncPostBackTrigger ControlID="btnInfo" EventName="Command" />
                    <asp:AsyncPostBackTrigger ControlID="btnExport" EventName="Command" />
                </Triggers>
                <ContentTemplate>
                    <asp:Panel ID="panSummary" runat="server" ClientIDMode="Static">
                        <div style="width: 700px; height: 300px; margin: 20px; overflow:auto;">
                            <div class="tb-title">Entry Exceptions Summary</div>
                            <asp:GridView ClientIDMode="Static" runat="server" ID="gvEntryPivot" CssClass="grid" EnableSortingAndPagingCallbacks="false"  AutoGenerateColumns="false" OnRowDataBound="gvEntryPivot_DataBound" AllowSorting="true" OnSorting="gvEntryPivot_Sorting">
                                <Columns>
                                    <asp:BoundField SortExpression="Division" DataField="Division" HeaderText="" HeaderStyle-Width="170px" ItemStyle-CssClass="c2"></asp:BoundField>
                                    <asp:BoundField SortExpression="today" DataField="today" HeaderText="" HeaderStyle-Width="170px" ItemStyle-CssClass="c2"></asp:BoundField>
                                    <asp:BoundField SortExpression="day1" DataField="day1" HeaderText="" HeaderStyle-Width="170px" ItemStyle-CssClass="c2"></asp:BoundField>
                                    <asp:BoundField SortExpression="day2" DataField="day2" HeaderText="" HeaderStyle-Width="170px" ItemStyle-CssClass="c2"></asp:BoundField>
                                    <asp:BoundField SortExpression="day3" DataField="day3" HeaderText="" HeaderStyle-Width="170px" ItemStyle-CssClass="c2"></asp:BoundField>
                                    <asp:BoundField SortExpression="day4" DataField="day4" HeaderText="" HeaderStyle-Width="170px" ItemStyle-CssClass="c2"></asp:BoundField>
                                    <asp:BoundField SortExpression="day5" DataField="day5" HeaderText="" HeaderStyle-Width="170px" ItemStyle-CssClass="c2"></asp:BoundField>
                                    <asp:BoundField SortExpression="day6" DataField="day6" HeaderText="" HeaderStyle-Width="170px" ItemStyle-CssClass="c2"></asp:BoundField>
                                </Columns>
                            </asp:GridView>
                        </div>
                    </asp:Panel>
                    </ContentTemplate>
            </asp:UpdatePanel>    

我在 Page_Init 中的事件订阅

    protected void Page_Init(object sender, EventArgs e)
    {
        try
        {
            btnSummary.Command += btnTabCommand;
            btnEntry.Command += btnTabCommand;
            btnOverdue.Command += btnTabCommand;
            btnInfo.Command += btnTabCommand;
            gvEntryPivot.Sorting += gvEntryPivot_Sorting;
        }
        catch(Exception ex)
        {
            messages.Error(ex.Message);
            Logger.Write(MessageType.Exception, ex.ToString());
        }
    }

我的活动代码

    protected void gvEntryPivot_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = gvEntryPivot.DataSource as DataTable;
        Trace.Warn("event");
        if(dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            gvEntryPivot.DataSource = dataView;
            gvEntryPivot.DataBind();
            panelGridView.Update();
        }
    }

转换函数

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch(sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

无论我做什么,事件都不会触发 - 我永远不会得到那个跟踪,也没有回发事件。页面上的其他按钮和事件工作正常。列标题不可点击,因为我希望可排序的 gridview 具有......我是否缺少一些关键设置?我在此发现的每篇帖子都是人们在事件触发后遇到问题,而不是让它进入触发期。

0 个答案:

没有答案