在asp.net portal starter kit中有在列表框中選擇指定項,通過點擊上下按鈕來實現排序的功能(如下圖)。
通常我想到的方法是(以上移為例):獲取選中項的排序號和選中項的上一項的排序號,交換它們的排序號即可。排序號的方式,就以1,2,3,4……的形式,新建的標簽的序號在最后一個的基礎上加一。在asp.net portal starter kit中采取的方式是:通過1,3,5,7……的形式來表示排序號,每一次增刪都重新構造排序號。在上移時,將當前的排序號減3,這樣新的排序號就在它原來上一位的前面,且在上兩位的后面。如:將第7位上移,那么新的順序是1,3,4,5,9。
代碼如下(部分代碼,詳細的可對照tabs.ascx.cs中的代碼看):
//上移下移代碼
if (tablist.selectedindex != -1)
{
int delta;
//因為標簽排序號是1,3,5,7,9方式排列的
//將一個標簽的上移或下移一位,就把標簽序號加減3,正好為一偶數就在前后一位的前面或后面。
//如:將第7位上移,那么新的順序是1,3,4,5,9
if (cmd == "down")
{
delta = 3;
}
else
{
delta = -3;
}
tabitem t;
t = (tabitem) portaltabs[tablist.selectedindex];
t.taborder += delta;
// 重新排序構造新的排序號
ordertabs();
}
/// <summary>
/// 將portaltabs中的標簽排序
/// </summary>
private void ordertabs ()
{
int i = 1;
// 使用指定的比較器對部分 system.collections.arraylist 中的元素進行排序。
// portaltabs中的對象是tabitem,tabitem對象繼承了icomparable接口,實現了以taborder的compareto
portaltabs.sort();
// renumber the order and save to database
// 將排序后的信息存入xml
foreach (tabitem t in portaltabs)
{
// 將標簽的排序號按1, 3, 5,遞增的順序排列
t.taborder = i;
i += 2;
// 將新的排序號寫入用戶配置文件
configuration config = new configuration();
config.updatetaborder(t.tabid, t.taborder);
}
}
這種方法其實我覺得并不好,只能算是一個新的思路。在ordertabs()時每次都要循環更新用戶配置文件,我覺的還不如,交換序號后在更新用戶配置文件。但是用交換序號的方式,要判斷是否選中項為第一項(不能上移)或是最后一項(不能下移)。而且程序中還有要將管理項作為最后一項的要求,用交換序號的方式可能又要多寫不少代碼。可能還有其他的好處我沒有想到,所以權衡利弊還是用他的方法吧!
順便說一個bug,好像默認提供的程序不能實現上移下移的功能,不知大家遇到了沒有。要將configuration.cs文件中savesitesettings()的方法修改一下才行,修改后的代碼:
public void savesitesettings()
{
// 原來的:從cache中獲取站點設置信息數據集(好像是個bug,因為每次更新數據是更新的httpcontext.current.items中的)
//siteconfiguration sitesettings = (siteconfiguration) httpcontext.current.cache["sitesettings"];
// 修改后的
siteconfiguration sitesettings = (siteconfiguration) httpcontext.current.items["sitesettings"];
// 如果cache中沒有,則重新構建
if(sitesettings == null)
{
// if savesitesettings() is called once, the cache is cleared. if it is
// then called again before global.application_beginrequest is called,
// which reloads the cache, the sitesettings object will be null
// (這一句不知翻譯的對不對,好像很重要)如果savesitesettings()被調用過一次后,cache就回被清除。如果它再一次被調用在global.application_beginrequest前sitesettings為null則重新寫cache
sitesettings = getsitesettings();
}
string configfile = httpcontext.current.server.mappath(configurationsettings.appsettings["configfile"]);
// object is evicted from the cache here.
// 將變更后的數據集寫入到xml文件
sitesettings.writexml(configfile);
}
更多相關內容:點擊這里>>
新聞熱點
疑難解答
圖片精選