下面的代码显示了在对GridView进行排序时添加上升 - 下降图像,但GridView SortExpression返回NULL。
知道为什么会这样吗?
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);
}
}
}
}
}
}
答案 0 :(得分:2)
我不认为在创建行时会设置它。单击排序列时会设置它。