开发者

Gridview RowCommand event is fired when I do sorting in gridview

开发者 https://www.devze.com 2023-03-25 06:13 出处:网络
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.

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">
                <Page开发者_如何学GorSettings 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???


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")
{
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号