不使用SqlDataSource的Sorting方式
因為資料來源不是從SqlDataSource取得,所以Sorting的時後要自已用ViewState去記錄選擇的是那個LinkButton及方向,如果使用SqlDataSource就不會有這個問題。
<asp:GridView ID="GridView1" runat="server"
onrowdatabound="GridView1_RowDataBound" AllowPaging="True"
onpageindexchanging="GridView1_PageIndexChanging"
AutoGenerateColumns="False"
CellPadding="4" AllowSorting="true"
onsorting="GridView1_Sorting"
onrowcreated="GridView1_RowCreated">
</asp:GridView>
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell tc in e.Row.Cells)
{
if (tc.HasControls() == true)
{
if (((LinkButton)tc.Controls[0]).Text == "Duration")
{
((LinkButton)tc.Controls[0]).Text = GetLocalResourceObject("Duration").ToString();
}
else if (((LinkButton)tc.Controls[0]).Text == "RecordingName")
{
((LinkButton)tc.Controls[0]).Text = GetLocalResourceObject("RecordingName").ToString();
}
else if (((LinkButton)tc.Controls[0]).Text == "FileSize")
{
((LinkButton)tc.Controls[0]).Text = GetLocalResourceObject("FileSize").ToString();
}
if (((LinkButton)tc.Controls[0]).CommandArgument == ViewState["SortDir"].ToString())
{
if (ViewState["mySorting"].ToString() == "ASC")
{
tc.Controls.Add(new LiteralControl("↓"));
}
else
{
tc.Controls.Add(new LiteralControl("↑"));
}
}
}
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["mySorting"] == null)
{
e.SortDirection = SortDirection.Ascending;
ViewState["mySorting"] = "ASC";
}
else
{
if (ViewState["mySorting"].ToString() == "ASC")
{
e.SortDirection = SortDirection.Descending;
ViewState["mySorting"] = "DESC";
}
else
{
e.SortDirection = SortDirection.Ascending;
ViewState["mySorting"] = "ASC";
}
ViewState["SortDir"] = e.SortExpression;
}
GetGridView1Data();
}
0 意見: