2011-07-25 42 views
1

下面的代碼顯示了在對GridView進行排序時添加上下行圖像,但GridView的SortExpression返回NULL。Gridview SortExpression爲空

任何想法爲什麼會發生這種情況?

protected void grvSample_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      foreach (TableCell tc in e.Row.Cells) 
      { 
       if (tc.HasControls()) 
       { 
        LinkButton lnk = (LinkButton)tc.Controls[0]; 
        if (lnk != null) 
        { 
         System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); 
         img.ImageUrl = "~/Images/" + (grvSample.SortDirection == SortDirection.Ascending ? "asc" : "desc") + ".gif"; 
         if (grvSample.SortExpression == lnk.CommandArgument) 
         { 
          // adding a space and the image to the header link 
          tc.Controls.Add(new LiteralControl(" ")); 
          tc.Controls.Add(img); 
         } 
        } 
       } 
      } 
     } 
    } 

回答

2

我不認爲它會在創建行時設置。它在點擊排序列時設置。