C# Gridview单击标题进行排序触发行命令

C# Gridview单击标题进行排序触发行命令,c#,asp.net,.net,asp.net-4.0,C#,Asp.net,.net,Asp.net 4.0,我有一个相当简单的GridView。这是列的标记: <Columns> <asp:TemplateField HeaderText="JD Name" SortExpression="FullName" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="180px" > <ItemTemplate

我有一个相当简单的GridView。这是列的标记:

 <Columns>
                <asp:TemplateField HeaderText="JD Name" SortExpression="FullName"
                    HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="180px" >
                    <ItemTemplate>
                        <asp:LinkButton CommandName="edt" CommandArgument='<%#Eval("JurisdictionID") %>' runat="server" Text='<%#Eval("FullName") %>' />
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:BoundField HeaderText="JD Abbreviation" ItemStyle-Width="200px"  DataField="JDAbbreviation" SortExpression="JDAbbreviation"
                    HeaderStyle-HorizontalAlign="Center" />

                 <asp:TemplateField 
                    HeaderStyle-HorizontalAlign="Center" >
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkStat" CommandName="inac" CommandArgument='<%#Eval("JurisdictionID") %>' 
                        runat="server" Text='<%#Utils.GetStatusString((bool) Eval("IsActive")) %>' />
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>


然而,当我点击一个列进行排序时,它首先触发一个row命令事件,然后触发排序事件。有人能告诉我我犯了什么错误吗?在RowCommand参数中,我得到SortExpression。这对我来说真的很有趣

排序
是一个
行命令
。查看此MSDN以了解更多详细信息

在row命令事件中,您应该添加一个
if
语句,以便确定row命令代码应该在何时执行。使用e.CommandName

void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
  // If multiple buttons are used in a GridView control, use the
  // CommandName property to determine which button was clicked.
  if(e.CommandName=="Add")
  {
    // Convert the row index stored in the CommandArgument
    // property to an Integer.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the button clicked 
    // by the user from the Rows collection.
    GridViewRow row = ContactsGridView.Rows[index];

    // Create a new ListItem object for the contact in the row.     
    ListItem item = new ListItem();
    item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " +
    Server.HtmlDecode(row.Cells[3].Text);

    // If the contact is not already in the ListBox, add the ListItem 
    // object to the Items collection of the ListBox control. 
    if (!ContactsListBox.Items.Contains(item))
    {
      ContactsListBox.Items.Add(item);
    }
  }
}