星期三, 3月 10, 2010

listview sorting

今天客戶有個需求,他希望可以做出像gridview那樣可以每個column都可以sorting,可是客戶的table又很ㄋㄧㄠ,所以我決定要用ListView這個control來做出效果,這裡用簡單的方式做出來,在這紀錄一下..看code吧..

.aspx

<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="ph">
<LayoutTemplate>
<table>
<thead>
<tr>
<th>
<asp:LinkButton ID="lbtnID" runat="server"
Text="客戶編號" CommandName="sorting" CommandArgument="客戶編號"></asp:LinkButton>
</th>
<th>
<asp:LinkButton ID="lbtnName" runat="server"
Text="公司名稱" CommandName="sorting" CommandArgument="公司名稱"></asp:LinkButton>
</th>
<th>
<asp:LinkButton ID="lbtnContact" runat="server"
Text="連絡人" CommandName="sorting" CommandArgument="連絡人"></asp:LinkButton>
</th>
</tr>
</thead>
<tbody>
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Literal ID="litT1" runat="server" Text='<%# Eval("客戶編號") %>' />
</td>
<td>
<asp:Literal ID="litT2" runat="server" Text='<%# Eval("公司名稱") %>' />
</td>
<td>
<asp:Literal ID="litT3" runat="server" Text='<%# Eval("連絡人") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>

.cs

public partial class listview : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ListView1.LayoutCreated += new EventHandler(ListView1_LayoutCreated);
}
/// <summary>
/// 連線字串
/// </summary>
string ConnectionString
{
get
{
return ConfigurationManager
.ConnectionStrings["cNorthWindConnectionString"].ConnectionString;
}
}
/// <summary>
/// 排序的物件
/// </summary>
Sorter SortObj
{
get
{
if (ViewState["SortObj"] != null)
{
return ViewState["SortObj"] as Sorter;
}
else
{
var sortObj = new Sorter();
sortObj.SetColumnName("客戶編號");
sortObj.SetSort();
ViewState["SortObj"] = sortObj;
return sortObj;
}
}
}
/// <summary>
/// 按鈕觸發的function
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void lbtn_Command(object sender, CommandEventArgs e)
{
//排序
if (e.CommandName == "sorting")
{
SortObj.SetColumnName(e.CommandArgument.ToString());
ToDataBind();
}
}
/// <summary>
/// 塞資料
/// </summary>
void ToDataBind()
{
using (var db = new B.Framework.DataBase.ADO.ADOHelper(ConnectionString))
{
string SQL = SortObj.GetSortingSQL("select * from 客戶");
ListView1.DataSource = db.ExecuteTable(SQL);
ListView1.DataBind();
}

}
void ListView1_LayoutCreated(object sender, EventArgs e)
{
LinkButton lbtnID = ListView1.FindControl("lbtnID") as LinkButton;
LinkButton lbtnName = ListView1.FindControl("lbtnName") as LinkButton;
LinkButton lbtnContact = ListView1.FindControl("lbtnContact") as LinkButton;
lbtnName.Command += new CommandEventHandler(lbtn_Command);
lbtnID.Command += new CommandEventHandler(lbtn_Command);
lbtnContact.Command += new CommandEventHandler(lbtn_Command);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitForm();
}
}
/// <summary>
/// 初始化
/// </summary>
private void InitForm()
{
ToDataBind();
}
}

class

/// <summary>
/// 處理排序的class
/// </summary>
[Serializable]
public class Sorter
{
string SortingName;
string SortingState;
//string SortingString;
/// <summary>
/// 取得排序的SQL
/// </summary>
/// <param name="SQL"></param>
/// <returns></returns>
public string GetSortingSQL(string SQL)
{
string template = @"
select *
from (
{0}
) as t
{1}
"
;
string sortingString = string.Format("order by {0} {1}", SortingName, SortingState);
return string.Format(template, SQL, sortingString);
}

public string GetSearchedSortingSQL(string SQL)
{
string template = @"
{0}
{1}
"
;
string sortingString = string.Format("order by {0} {1}", SortingName, SortingState);
return string.Format(template, SQL, sortingString);
}

/// <summary>
/// 設定正向排序
/// </summary>
public void SetSort()
{
SortingState = "";
}
/// <summary>
/// 設定反向排序
/// </summary>
public void SetSortDesc()
{
SortingState = "desc";
}
/// <summary>
/// 設定哪個欄位排序
/// </summary>
/// <param name="sortingColumnName"></param>
public void SetColumnName(string sortingColumnName)
{
if (sortingColumnName != SortingName)
{
SortingName = sortingColumnName;
SortingState = "";
}
else
{
if (SortingState == "")
{
SortingState = "desc";
}
else
{
SortingState = "";
}
}
}
}

效果圖:

image

參考:

星期二, 3月 02, 2010

清除全部的 cache

今天在處理客戶的需求, 客戶有很多功能都有用到 cache, 可是客戶說在測試網站功能時,又不希望有 cache, 因為有 cache 就要等 cahce 過期後才能讀到資料厙的新資料, 所以需要寫個程式去清理 cache 內的所有資料, 看了一下 cache 的 method 並沒有提供清除全部 cache 的 function, 所以就自己土砲寫一下吧..

List<string> removeList = new List<string>();
foreach (DictionaryEntry cacheItem in HttpRuntime.Cache)
{
removeList.Add(cacheItem.Key.ToString());
}
foreach (string key in removeList)
{
HttpRuntime.Cache.Remove(key);
}

這樣就可以完成需求啦..^_____^..

參考:
http://msdn.microsoft.com/zh-tw/library/system.collections.dictionaryentry.aspx
http://msdn.microsoft.com/zh-tw/library/system.web.caching.cache%28VS.80%29.aspx