当我在Gridview中排序时,Gridview RowCommand事件被触发



我被一些异常问题困住了。当我在gridview中排序时,它为那个网格触发RowCommand事件,而不是排序事件。下面是我的网格视图的HTML代码。

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

//下面是处理这些事件的代码。

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

如何解决这个问题??

您是否尝试检查grdDefects_RowCommand中的commandName

RowCommand事件将触发每当你点击GridView中的任何按钮,无论是在头部或普通行。如果发生排序事件,请阻止代码执行。

将RowCommand事件中的代码移到此块

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

相关内容

  • 没有找到相关文章

最新更新