淺談ASP.NET中的TreeView
ASP.NET 2.0 的 TreeView 控件功能雖說強(qiáng)大,但其客戶端控制很遜色,本文將講解 TreeView 的客戶端實(shí)現(xiàn)原理,并實(shí)現(xiàn)兩個(gè)個(gè)性化操作:
(1) 節(jié)點(diǎn)的全部打開和關(guān)閉;
Client Side Expand/Collapse All Nodes For ASP.NET 2.0 TreeView.
(2) 只打開一個(gè)節(jié)點(diǎn)(關(guān)閉其他兄弟節(jié)點(diǎn))。
Just one expanded node in ASP.NET 2.0 TreeView (When a client expand one node all other will collaps)
用記事本打開頁(yè)面源代碼,可以找到一下兩個(gè)腳本引用:
- <script src="/WebUI/WebResource.axd?d=RAQeBcDUNuP9iuS8q3tNEw2&
t=633300220640000000" type="text/javascript"></script>- <script src="/WebUI/WebResource.axd?d=JuTdJhq3NM8Jq_RhssAkEg2&
t=633300220640000000" type="text/javascript"></script>
將"/WebUI/WebResource.axd?d=RAQeBcDUNuP9iuS8q3tNEw2& amp;t=633300220640000000"拷到地址欄尾,下載腳本,并以 .js 命名,另一個(gè)同樣操作。分析第二個(gè)腳本文件,可以看到TreeView的很多客戶端函數(shù),其中關(guān)鍵的一個(gè) TreeView_ToggleNode 就是客戶端點(diǎn)擊時(shí)觸發(fā)的事件。
要想做個(gè)性化的操作,就得從 TreeView_ToggleNode 事件下手。我們無(wú)法更改.net封裝好的腳本,只有“重寫”。所謂的重寫就是在原來(lái)的函數(shù)之后添加一個(gè)同名函數(shù)(因?yàn)閖s對(duì)于同名函數(shù)只調(diào)用***一個(gè))。
TreeView_ToggleNode 的原函數(shù):
- function TreeView_ToggleNode(data, index, node, lineType, children) {
- var img = node.childNodes[0];
- var newExpandState;
- try {
- if (children.style.display == "none") {
- children.style.display = "block";
- newExpandState = "e";
- if ((typeof(img) != "undefined") && (img != null)) {
- if (lineType == "l") {
- img.src = data.images[15];
- }
- else if (lineType == "t") {
- img.src = data.images[12];
- }
- else if (lineType == "-") {
- img.src = data.images[18];
- }
- else {
- img.src = data.images[5];
- }
- img.alt = data.collapseToolTip.replace(/\{0\}/, TreeView_GetNodeText(node));
- }
- }
- else {
- children.style.display = "none";
- newExpandState = "c";
- if ((typeof(img) != "undefined") && (img != null)) {
- if (lineType == "l") {
- img.src = data.images[14];
- }
- else if (lineType == "t") {
- img.src = data.images[11];
- }
- else if (lineType == "-") {
- img.src = data.images[17];
- }
- else {
- img.src = data.images[4];
- }
- img.alt = data.expandToolTip.replace(/\{0\}/, TreeView_GetNodeText(node));
- }
- }
- }
- catch(e) {}
- datadata.expandState.value = data.expandState.value.substring(0, index) +
newExpandState + data.expandState.value.slice(index + 1);- }
【編輯推薦】