4

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

Pavitar
  • 4,282
  • 10
  • 50
  • 82
Microsoft Developer
  • 5,229
  • 22
  • 93
  • 142
  • please post your code where you have handled the row_command and sorting events – Pavitar Aug 05 '11 at 06:26
  • @Pavitar. I have updated my question. You can see the code now. – Microsoft Developer Aug 05 '11 at 06:31
  • 1
    Try debugging ,set a break point at statement...at `if(e.CommandName == "Update")` in the grdDefects_RowCommand event,as in your case and check if that is the one getting fired.Do you have any other Command Fields? – Pavitar Aug 05 '11 at 06:48
  • Sorting and Paging are bubbled up and go thru the Rowcommand. That is normal. What you are doing wrong is not wrapping your code in an if block that checks for your specific command name. if(e.RowCommand == "redirect"){ ... } – Vaibhav Garg Aug 05 '11 at 07:40

1 Answers1

26

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")
{
}
dividius
  • 820
  • 6
  • 16
Munawar
  • 2,588
  • 2
  • 26
  • 29
  • No I have not checked but as I have handled sorting event of grid view, it must be fired when I do sorting. Its working perfectly in other pages. – Microsoft Developer Aug 05 '11 at 06:44
  • @Needo please add a close the double quotes after `if(e.CommandName != "Sort")` If he does this, he will not know why the grdDefects_RowCommand is getting called , and which one CommandName is called.Because as it is, his sorting does not work.So this will take him no where – Pavitar Aug 05 '11 at 07:16