Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

c# - Gridview Event Subscription

I have been looking through several posts trying to figure out what I have incorrect in my gridview. I am trying to allow sorting. I have ensured EnableSortingAndPagingCallbacks is set properly

EnableSortingAndPagingCallbacks="false" 

As well as these

AllowSorting="true" OnSorting="gvEntryPivot_Sorting"

And I have this before this element in the page

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

My HTML code

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

My event subscription in 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());
        }
    }

My event code

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

The conversion function

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

No matter what I do, the event never triggers - I never get that trace, and there is no postback event. Other buttons and events on the page work just fine. The column headers are not clickable as I would expect a sortable gridview to have.. Am I missing some key setting? Every post I find on this is people having issues after the event fires, not in getting it to fire period.

question from:https://stackoverflow.com/questions/65848668/gridview-event-subscription

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...