gridview 点击列标题后,列标题不能变成链接,不能触发GridView_Department_Sorting事件,为什么呢?
请大侠告诉我。
前台代码如下:
         <asp:GridView ID="GridView_Department" runat="server"   AllowPaging="true"  AllowSorting="true"
                 AutoGenerateColumns="False"    onsorting="GridView_Department_Sorting" SelectedIndex="0" >  
                             <Columns >
                                <asp:CommandField ShowSelectButton="True" SelectText="选择"  />
                                <asp:BoundField DataField="DEP_ID" HeaderText="部门编号" Visible="false">
                                    <ItemStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                                <asp:BoundField DataField="DEP_NAME" HeaderText="部门名称" >
                                    <ItemStyle HorizontalAlign="Left" />
                                </asp:BoundField>
                          </Columns>
                        </asp:GridView>
后台代码如下:
protected void GridView_Department_Sorting(object sender, GridViewSortEventArgs e)
    { string sortExpression = e.SortExpression.ToString();
              string sortDirection = "desc";
               if (sortExpression == GridView_Department.Attributes["SortExpression"])
        {
            if (this.GridView_Department.Attributes["SortDirection"].ToString() == "asc")
                sortDirection = "desc";
            else
                sortDirection = "asc";
        }
         GridView_Department.Attributes["SortExpression"] = sortExpression;
        GridView_Department.Attributes["SortDirection"] = sortDirection;
        Database_Con();
        GetGVData();
        connection.Close();
}

解决方案 »

  1.   

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="GridView1_Sorting" AllowPaging="true" PageSize="10" OnPageIndexChanging="GridView1_PageIndexChanging">   
      <Columns>   
      <asp:BoundField DataField="id" HeaderText="代码" SortExpression="id"/>   
      <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name"/>   
      <asp:TemplateField SortExpression="BUID" HeaderText="BUID">   
      <ItemTemplate>   
      <asp:Label ID="Label1" runat="server" Text=' <%# Bind("ID") %>' ToolTip=' <%# DataBinder.Eval(Container.DataItem, "ID") %>'> </asp:Label>   
      </ItemTemplate>   
      </asp:TemplateField>   
      </Columns>   
      </asp:GridView>   protected void Page_Load(object sender, EventArgs e)   
      {   
      if (!this.IsPostBack)   
      {   
      ViewState["SortOrder"] = "deptid";   
      ViewState["OrderDire"] = "asc";   
      BindGridView();   
      }   
      }     protected void BindGridView()   
      {   
        
      DataSet ds = new DataSet();   
          DataView dv = ds.Tables[0].DefaultView;   
      string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];   
      dv.Sort = sort;   
      this.GridView1.DataSource = dv;     this.GridView1.DataBind();     }   
      protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)   
      {   
      string vortExp = e.SortExpression;   
      if (ViewState["SortOrder"].ToString() == vortExp)   
      {   
      if ((string)ViewState["OrderDire"] == "desc")   
      {   
      ViewState["OrderDire"] = "asc";   
      }   
      else if ((string)ViewState["OrderDire"] == "asc")   
      {   
      ViewState["OrderDire"] = "desc";   
      }   
      }   
      else   
      {   
      ViewState["SortOrder"] = e.SortExpression;   
      }   
      BindGridView();     }   
      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)   
      {   
      this.GridView1.PageIndex = e.NewPageIndex;   
      BindGridView();   
      }