Gridview RowCommand event is fired when I do sorting in gridview

一个人想着一个人 提交于 2019-12-13 14:29:09

问题


I am trapped in some abnormal problem. When I do sorting in gridview, it fires RowCommand event for that grid instead of sorting event. Below is the HTML code for my grid view.

<asp:GridView ID="grdDefects" runat="server" AutoGenerateColumns="False"    OnPageIndexChanging="grdDefects_PageIndexChanging"
                OnSorting="grdDefects_Sorting" OnRowCommand="grdDefects_RowCommand"  AllowSorting="true">
                <PagerSettings Mode="NumericFirstLast" FirstPageText="First"  LastPageText="Last"
                    NextPageText="Next" PreviousPageText="Prev" />
                <Columns>
                    <%--<asp:TemplateField HeaderText="Id" SortExpression="ReasonID" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblReasonID" runat="server" Text='<%#  Bind("ReasonID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>--%>
                    <asp:BoundField DataField="DefectId" HeaderText="Id" />
                    <asp:BoundField DataField="DefectName" HeaderText="Defect"  sortExpression="DefectName" />
                    <asp:BoundField DataField="Department" HeaderText="Department Name" sortExpression="Department" />

                   <%-- <asp:ButtonField ControlStyle-CssClass="btns" ButtonType="Button" CommandName="Update"
                        Text="Edit" >
<ControlStyle CssClass="btns"></ControlStyle>
                    </asp:ButtonField>--%>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="editBtn" runat="server" Text="EDIT"  CommandArgument='<%# Eval("DefectId") %>' CssClass="btns"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

//Here is the code to handle those events.

protected void grdDefects_Sorting(object sender, GridViewSortEventArgs e)
{
    try
    {
        if (ViewState["sortMode"] == null)
        {
            ViewState["sortMode"] = strSORT_DESC;
        }
        else if(ViewState["sortMode"]!=null)
        {
            if (ViewState["sortMode"].ToString().Equals("strSORT_ASC"))
                ViewState["sortMode"] = strSORT_DESC;
            else
                ViewState["sortMode"] = strSORT_ASC;
        }
        //string strSortExpression = e.SortExpression;
        ViewState["sortExpression"] = e.SortExpression;
        sort();

    }
    catch (Exception ex)
    {
        throw ex;
    }  
}

protected void grdDefects_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        int Id = Convert.ToInt32(e.CommandArgument);
        Response.Redirect("AddDefect.aspx?Id=" + Id);
    }
    catch (Exception ex)
    {

        throw;
    }
}

How to solve this problem???


回答1:


Did you try checking commandName in grdDefects_RowCommand

RowCommand events will fire whenever you click any button in GridView, whether its in header or in normal row. Just prevent your code from execution if its sorting event.

move the code from RowCommand event into this block

If (e.CommandName !="Sort")
{
}


来源:https://stackoverflow.com/questions/6952256/gridview-rowcommand-event-is-fired-when-i-do-sorting-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!