我有一个GridView,我在其中显示文件夹中的一些文件。我能够根据排序添加两个列标题的向上和向下箭头进行更新。点击两次列标题后,箭头显示出来。
GridView的:
<asp:GridView ID="GridView1" AllowSorting="true" OnSorting="GridView1_Sorting" ClientIDMode="Static" runat="server" AutoGenerateColumns="false" EmptyDataText="No PDF was generated" OnRowCreated="GridView1_RowCreated">
<Columns>
<asp:BoundField DataField="Text" HeaderText="File Name" SortExpression="FileName" />
<asp:BoundField DataField="Value" HeaderText="File Modified Date" SortExpression="FileDate" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="Download" runat="server" CommandArgument='<%# Container.DataItemIndex %>' OnClick="DownloadFile" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkView" Text="View in Browser" CommandArgument='<%# Container.DataItemIndex %>' OnClientClick="window.document.forms[0].target='blank';" runat="server" OnClick="ViewFile" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
的Page_Load:
if (!Page.IsPostBack) //loaded first time...
{
//MessageBox.Show("Fires from a PAGE REFRESH or FIRST VISIT");
BindData();
}
支持功能:
protected void BindData()
{
try
{
filePaths = Directory.GetFiles(@"C:\PDFGenerate");
files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), File.GetLastWriteTime(filePath).ToString()));
}
//GridView1.DataSource = files;
//GridView1.DataBind();
Session["fileData"] = files;
Sort(files, SortDirection.Descending);
}
catch (Exception ce)
{
}
//MessageBox.Show(files.Count() + ""); // displays the count for the files being displayed
}
public void Sort(IList<ListItem> items, SortDirection direction)
{
var dataTable = ToDataTable(items);
var dataView = new DataView(dataTable);
dataView.Sort = string.Format("{0} {1}", "Value",
direction == SortDirection.Ascending ? "ASC" : "DESC");
GridView1.DataSource = dataView;
GridView1.DataBind();
}
public DataTable ToDataTable(IList<ListItem> data)
{
var table = new DataTable();
table.Columns.Add("Value", typeof(string));
table.Columns.Add("Text", typeof(string));
foreach (var item in data)
{
var row = table.NewRow();
row["Value"] = item.Value;
row["Text"] = item.Text;
table.Rows.Add(row);
}
return table;
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
LinkButton lbSort;
Image imgArrow;
foreach (TableCell cell in e.Row.Cells)
{
if (cell.HasControls())
{
lbSort = (LinkButton)cell.Controls[0];
imgArrow = new Image();
if (ViewState["SortColumn"] != null)
{
if (lbSort.CommandArgument == ViewState["SortColumn"].ToString())
{
if (Convert.ToInt32(ViewState["SortColumnDirection"]) == (int)SortDirection.Ascending)
{
imgArrow.ImageUrl = "down.png";
//MessageBox.Show("UP");
}
else
{
imgArrow.ImageUrl = "up.png";
//MessageBox.Show("DOWN");
}
cell.Controls.Add(imgArrow);
}
}
}
}
}
}
protected void GridView1_Sorting(object server, GridViewSortEventArgs e)
{
string strSortExpression = e.SortExpression;
MessageBox.Show(e.SortExpression.ToString());
MessageBox.Show(e.SortDirection.ToString());
switch (strSortExpression)
{
case "FileName":
if (e.SortExpression == (string)ViewState["SortColumn"])
{
// We are resorting the same column, so flip the sort direction
e.SortDirection =
((SortDirection)ViewState["SortColumnDirection"] == SortDirection.Ascending) ?
SortDirection.Descending : SortDirection.Ascending;
}
Sort2((IList<ListItem>)Session["fileData"], e.SortDirection, "filename");
ViewState["SortColumn"] = e.SortExpression;
ViewState["SortColumnDirection"] = e.SortDirection;
break;
case "FileDate":
if (e.SortExpression == (string)ViewState["SortColumn"])
{
// We are resorting the same column, so flip the sort direction
e.SortDirection =
((SortDirection)ViewState["SortColumnDirection"] == SortDirection.Ascending) ?
SortDirection.Descending : SortDirection.Ascending;
}
Sort2((IList<ListItem>)Session["fileData"], e.SortDirection, "filedate");
ViewState["SortColumn"] = e.SortExpression;
ViewState["SortColumnDirection"] = e.SortDirection;
break;
}
}
public void Sort2(IList<ListItem> items, SortDirection direction, string type)
{
var dataTable = ToDataTable(items);
var dataView = new DataView(dataTable);
switch (type)
{
case "filename":
dataView.Sort = string.Format("{0} {1}", "Text",
direction == SortDirection.Ascending ? "ASC" : "DESC");
GridView1.DataSource = dataView;
GridView1.DataBind();
break;
case "filedate":
dataView.Sort = string.Format("{0} {1}", "Value",
direction == SortDirection.Ascending ? "ASC" : "DESC");
GridView1.DataSource = dataView;
GridView1.DataBind();
break;
}
}
如何在页面加载时显示箭头? (我是 在页面加载时按文件修改日期排序)
为什么箭头会在相应的标题上点击两次以显示? (虽然排序是正确的,但是第一次单击不同的标题列时箭头显示错误,需要额外点击一次才能正确显示图像) 例如:
答案 0 :(得分:1)
在页面加载时你没有得到图像,因为
ViewState["SortColumn"]
为null,在GridView_Sorting
函数中设置(在page_load上未调用)
将其设置为默认值
再次,您无法在第一次排序点击时设置图像,因为在 GridView_Sorting 功能之前调用了 RowCreated
所以下次点击它时,row_created会找到该值并设置图像