当前位置 : 主页 > 网络编程 > ASP >

asp.net – 当我在gridview中进行排序时触发了Gridview RowCommand事件

来源:互联网 收集:自由互联 发布时间:2021-06-24
我陷入了一些异常的问题.当我在gridview中进行排序时,它会触发该网格的RowCommand事件而不是排序事件.下面是我的网格视图的 HTML代码. asp:GridView ID="grdDefects" runat="server" AutoGenerateColumns="
我陷入了一些异常的问题.当我在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

无论何时单击GridView中的任何按钮,无论是在标题中还是在正常行中,都会触发RowCommand事件.如果排序事件,请阻止您的代码执行.

将代码从RowCommand事件移动到此块中

If (e.CommandName !="Sort")
{
}
网友评论