欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net 自制的單選、多選列表實(shí)現(xiàn)代碼

 更新時(shí)間:2009年08月21日 11:35:31   作者:  
在ASP.NET的頁面上,ListBox最終是渲染成select元素,而CheckListBox最終被渲染成div或者是table,使得二者的樣式無法統(tǒng)一,或者說要統(tǒng)一很麻煩。
問:為什么要“自制”?不是有現(xiàn)成的控件嗎?
答:在ASP.NET的頁面上,ListBox最終是渲染成select元素,而CheckListBox最終被渲染成div或者是table,使得二者的樣式無法統(tǒng)一,或者說要統(tǒng)一很麻煩。
解決:
于是,決定干脆自行組合一些元素,實(shí)現(xiàn)單選列表、多選列表的統(tǒng)一樣式。
首先,無論是單選列表還是多選列表,都用一個(gè)有邊框的div來做容器:
<div class="list"></div>
然后,在這個(gè)div中添加數(shù)據(jù)項(xiàng)。為了在響應(yīng)onclick事件時(shí),能夠遍歷數(shù)據(jù)項(xiàng),進(jìn)而做一些樣式上的控制,我需要把各個(gè)數(shù)據(jù)項(xiàng)的name屬性設(shè)為一樣的,然后用getElementsByName獲?。ㄟ@種辦法我在復(fù)選框的全選功能上常用)。然而,經(jīng)過實(shí)踐,發(fā)現(xiàn)div、span均無name屬性,最終找到用錨點(diǎn),也就是<a>標(biāo)記,可以實(shí)現(xiàn)。
例如:
復(fù)制代碼 代碼如下:

<div id="divDepartments" class="list">
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門1</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門2</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門3</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門4</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門5</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門6</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門7</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門8</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門9</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門10</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門11</a>
<a name="aDep" style="width:100%" onclick="ItemClicked(this);">部門12</a>
</div>

其中,list樣式:
復(fù)制代碼 代碼如下:

.list
{
overflow-y:scroll;
width:120px;
height:150px;
padding:3px;
border:solid 1px #AFAFAF;
background-color: #ffffff;
cursor: pointer;
}

ItemClicked函數(shù)用來響應(yīng)click事件。下面的代碼只是做一些樣式上的變化,還可繼續(xù)添加加載數(shù)據(jù)的內(nèi)容:
復(fù)制代碼 代碼如下:

function ItemClicked(a){
a.style.backgroundColor="#EEEEEE";
as=document.getElementsByName(a.name);
for(i=0;i<as.length;i++){
if(as[i]!=a){as[i].style.backgroundColor="#FFFFFF";}
}
}

帶有復(fù)選框的多選列表也大同小異,只是這里由于遍歷數(shù)據(jù)項(xiàng)時(shí),只要對(duì)復(fù)選框遍歷即可,故可以使用div做數(shù)據(jù)項(xiàng)的容器了:
復(fù)制代碼 代碼如下:

<div id="divPersons" class="list">
<div><input type="checkbox" />人員1</div>
<div><input type="checkbox" />人員2</div>
<div><input type="checkbox" />人員3</div>
<div><input type="checkbox" />人員4</div>
<div><input type="checkbox" />人員5</div>
<div><input type="checkbox" />人員6</div>
<div><input type="checkbox" />人員7</div>
<div><input type="checkbox" />人員8</div>
<div><input type="checkbox" />人員9</div>
<div><input type="checkbox" />人員10</div>
<div><input type="checkbox" />人員11</div>
</div>

最后,關(guān)于數(shù)據(jù)的加載問題,在當(dāng)前的具體問題中,我打算用Ajax.Updater,來實(shí)現(xiàn)對(duì)相應(yīng)列表的div中數(shù)據(jù)項(xiàng)的填充。

相關(guān)文章

最新評(píng)論