ASP.NET中WebForm組件CheckBoxList編程(4)
2024-07-10 12:55:47
供稿:網友
國內最大的酷站演示中心!
五. 文中源程序代碼(check.aspx)和執行的界面:
下圖是執行了下列源程序代碼(check.aspx)后,生成的界面:
check.aspx源程序代碼如下:
<% @ page language = "c#" %>
<html >
<head >
<title > checkboxlist組件演示程序 </title >
<script runat = "server" >
protected void button_click ( object sender , eventargs e )
{
//組件中的檢查框中的文本和選框的排列位置
switch ( cboalign . selectedindex )
{
case 0 :
chklist . textalign = textalign . left ;
break ;
case 1 :
chklist . textalign = textalign . right ;
break ;
}
//組件中各個檢查框布局
switch ( cborepeatlayout . selectedindex )
{
case 0 :
chklist . repeatlayout = repeatlayout . table ;
break ;
case 1 :
chklist . repeatlayout = repeatlayout . flow ;
break ;
}
//組件中各個檢查框排列方向
switch ( cborepeatdirection . selectedindex)
{
case 0 :
chklist . repeatdirection = repeatdirection . vertical ;
break ;
case 1 :
chklist . repeatdirection = repeatdirection . horizontal ;
break ;
}
//組件中各個檢查框的排列行數
try
{
int cols = int . parse ( txtrepeatcols.text ) ;
chklist . repeatcolumns = cols ;
}
catch ( exception )
{
}
lblresult . text = "" ;
for ( int i = 0 ; i < chklist . items . count ; i++ )
{
if( chklist . items [ i ] . selected )
{
lblresult . text += chklist . items [ i ] .text + " <br > " ;
}
}
}
</script >
</head >
<body >
<form runat = "server" >
<h1 align = center > checkboxlist組件演示程序 </h1 >
<table >
<tr >
<td > 組件中的文本排列位置: </td >
<td >
<asp:dropdownlist id = cboalign runat = "server" >
<asp:listitem > 居左 </asp:listitem >
<asp:listitem > 居右 </asp:listitem >
</asp:dropdownlist >
</td >
</tr >
<tr >
<td > 組件中各個條目布局: </td >
<td >
<asp:dropdownlist id = cborepeatlayout runat = "server" >
<asp:listitem > 表格型 </asp:listitem >
<asp:listitem > 緊湊型 </asp:listitem >
</asp:dropdownlist >
</td >
</tr >
<tr >
<td> 組件中各個條目排列方向:</td >
<td >
<asp:dropdownlist id = cborepeatdirection runat = "server" >
<asp:listitem > 水平方向 </asp:listitem >
<asp:listitem > 垂直方向 </asp:listitem >
</asp:dropdownlist >
</td >
</tr >
<tr >
<td > 組件中各個條目排列行數: </td >
<td > <asp:textbox id = "txtrepeatcols" runat = "server" /> </td >
</tr >
</table >
<br >
請選擇你所需要學習的計算機語言類型:
<br >
<asp:checkboxlist id = "chklist" repeatdirection = horizontal runat = "server" >
<asp:listitem > visual c++ .net </asp:listitem >
<asp:listitem > visual c# </asp:listitem >
<asp:listitem > vb.net </asp:listitem >
<asp:listitem > jscript.net </asp:listitem >
<asp:listitem > visual j# </asp:listitem >
</asp:checkboxlist >
<br >
<asp:button text = "提交" runat = "server" onclick = "button_click" />
<h1 > <font color = red > 你選擇的計算機語言類型為: </font > </h1 >
<asp:label id = lblresult runat = "server" />
</form >
</body >
</html >
六. 總結:
其實checkboxlist組件也是一個服務器端組件。本文介紹了checkboxlist組件中的一些主要的屬性和方法,并且通過一個比較典型的例子說明了在asp.net頁面中如何進行與checkboxlist組件相關的編程,其實對于另外一個比較重要的組件--checkbox來說,他們中有許多的相似之處,掌握了checkboxlist組件的用法大致也就掌握了checkbox組件的用法。