当我在gridview中进行排序时会触发Gridview RowCommand事件 [英] Gridview RowCommand event is fired when I do sorting in gridview

查看:83
本文介绍了当我在gridview中进行排序时会触发Gridview RowCommand事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在一些异常问题中.当我在gridview中进行排序时,它会为该网格触发RowCommand事件,而不是排序事件.以下是我的网格视图的HTML代码.

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

如何解决这个问题?

推荐答案

您是否尝试在grdDefects_RowCommand中检查commandName

Did you try checking commandName in grdDefects_RowCommand

只要您单击GridView中的任何按钮(无论其在标题中还是在常规行中),都会触发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.

将代码从RowCommand事件移至该块中

move the code from RowCommand event into this block

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

这篇关于当我在gridview中进行排序时会触发Gridview RowCommand事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆