2013-05-16 24 views
0

我有一個過濾/排序功能正常工作的gridview。我的問題是onload,它不會在我的標題後顯示asc/desc圖像。只有在點擊任何標題後才能顯示排序圖像。任何幫助,將不勝感激。加入gridview標題圖像(asc/desc)onload

代碼隱藏:

#region Sorting Arrows for Gridview Header 
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.Header) 
    { 
     foreach (TableCell tc in e.Row.Cells) 
     { 
      // search for the header link 
      LinkButton lnk = (LinkButton)tc.Controls[0]; 
      if (tc.HasControls() && lnk != null && GridView1.SortExpression == lnk.CommandArgument) 
      { 
       // inizialize a new image 
       System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); 
       // setting the dynamically URL of the image 
       img.ImageUrl = "~/Contents/Images/" + (GridView1.SortDirection == SortDirection.Ascending ? "asc" : "desc") + ".png"; 
       // adding a space and the image to the header link 
       tc.Controls.Add(new LiteralControl(" ")); 
       tc.Controls.Add(img); 
       // 
      } 
     } 
    } 
} 
#endregion 

回答

2

我想通了..

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    {  
     Image img = new Image(); 
     img.ImageUrl = "~/Contents/Images/asc.png"; 
     GridView1.HeaderRow.Cells[1].Controls.Add(new LiteralControl(" ")); 
     GridView1.HeaderRow.Cells[1].Controls.Add(img); 
    } 
} 

謝謝..