国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

DataGrid Web控件深度歷險(xiǎn)(2) Part2

2024-07-21 02:24:24
字體:
供稿:網(wǎng)友
在本文的第一部分,我們研究了如何設(shè)定datagrid web控件的顯示屬性以及如何通過樣式設(shè)定datagrid的頁眉、頁腳、行和交替行的顯示。所有這些技術(shù)或是用于設(shè)定整個(gè)datagrid的顯示,或是用于設(shè)定datagrid中行的顯示。但是如何設(shè)定datagrid中列的顯示屬性?其實(shí)并不難,接著讀你就知道了。

設(shè)定哪些列應(yīng)該顯示

缺省情況下datagrid在生成的html表格中為sql查詢返回的每一列生成一個(gè)對(duì)應(yīng)的列。但是在一些情況下僅希望在datagrid中顯示這些列中的一部分列。例如,在我正在進(jìn)行的示例中,通過調(diào)用sp_popularity存儲(chǔ)過程顯示了aspfaqs.com最受歡迎的10個(gè)問題。它包含faqid列,或許我并不希望顯示該列。

如果不想在datagrid中顯示數(shù)據(jù)庫查詢返回的所有列,必須顯式地聲明所有希望顯示的列。第一步是將datagrid的autogeneratecolumns屬性設(shè)為false。一旦執(zhí)行完這個(gè)操作,就需要通過boundcolumn web控件設(shè)定需顯示的列,如下所示:

<asp:datagrid runat="server" autogeneratecolumns="false">
<columns>
<asp:boundcolumn datafield="databasecolumnname1" ... />
<asp:boundcolumn datafield="databasecolumnname2" ... />
...
<asp:boundcolumn datafield="databasecolumnnamen" ... />
</columns>
</asp:datagrid>
對(duì)于每一個(gè)希望顯示的列,需要通過一個(gè)包含datafield屬性的<asp:boundcolumn ... />標(biāo)記來指定數(shù)據(jù)庫中需要顯示的列。所有這些boundcolumn標(biāo)記必須包含在column標(biāo)記內(nèi)。(也可通過編程的方式指定這些綁定列,但是它的可讀性差,并且需要很多代碼!)請(qǐng)注意只有通過boundcolumn標(biāo)記指定的列才會(huì)在datagrid中顯示,你必須指定需要顯示的列!

boundcolumn控件的優(yōu)點(diǎn)在于它包含一些設(shè)定格式的屬性,包括:

l headertext — 設(shè)定列標(biāo)題的文字。

l footertext — 設(shè)定列尾的文字(記住若要在datagrid中顯示頁腳,應(yīng)將showfooter設(shè)為true)。

l headerstyle/footerstyle/itemstyle — 包含與datagrid樣式相同的屬性。對(duì)設(shè)定列居中、前景色、背景色等很有用。

l dataformatstring — 設(shè)置格式命令。(參考下面的示例;參考文檔以獲得全部的格式化規(guī)范)

讓我們看一下如何通過使用boundcolumn標(biāo)記來進(jìn)一步增強(qiáng)前面的示例。正如前面所提到的,我們不想顯示faqid或faqcategoryid列,并且我們希望對(duì)數(shù)字列(viewcount)和日期/時(shí)間列(dateentered)設(shè)定格式。另外,我們希望數(shù)字列的值居中。這些均可通過幾行易于閱讀易于理解的代碼完成:

<asp:datagrid runat="server" id="dgpopularfaqs"
backcolor="#eeeeee" width="85%"
horizontalalign="center"
font-name="verdana" cellpadding="4"
font-size="10pt" autogeneratecolumns="false">
<headerstyle backcolor="black" forecolor="white"
font-bold="true" horizontalalign="center" />
<alternatingitemstyle backcolor="white" />

<columns>
<asp:boundcolumn datafield="catname" headertext="category name" />
<asp:boundcolumn datafield="description" headertext="faq description" />
<asp:boundcolumn datafield="viewcount" dataformatstring="{0:#,###}"
headertext="views" itemstyle-horizontalalign="center" />
<asp:boundcolumn datafield="submittedbyname" headertext="author" />
<asp:boundcolumn datafield="submittedbyemail" headertext="author's email" />
<asp:boundcolumn datafield="dateentered" headertext="date added"
dataformatstring="{0:mm-dd-yyyy}" />
</columns>
</asp:datagrid>
實(shí)際運(yùn)行結(jié)果如下:

category name
faq description

views
date added

getting started
where can i host my asp web site for free (similar to geocities or tripod or any of the many other free web site sites)?

161,316
03-20-2001

asp.net
how can i format numbers and date/times using asp.net? for example, i want to format a number as a currency.

124,391
01-19-2002

databases, errors
i am using access and getting a 80004005 error (or a [microsoft][odbc microsoft access driver] the microsoft jet database engine cannot open the file '(unknown)' error) when trying to open a connection! how can i fix this problem?

108,374
01-17-2001








如上例所示,上述代碼指定了需要顯示的特定列并且應(yīng)用了特定的格式。請(qǐng)注意dataformatestring看上去很有趣。它的格式始終是{0:format string}。{0: …}指定通過格式化字符串(由…指定的)來格式化第一個(gè)參數(shù)(第一個(gè)參數(shù)指由datareader返回的那個(gè)特定列的值)。在示例中我使用了格式化字符串#,###,它在每3個(gè)數(shù)字前加上一個(gè)逗號(hào);格式化字符串mm-dd-yyyy指定通過月、日和年的格式顯示日期/時(shí)間字段。

結(jié)論

花一些時(shí)間看一下第一個(gè)示例(見datagrid web控件深度歷險(xiǎn)(1))和現(xiàn)在的示例。改進(jìn)確實(shí)很大!請(qǐng)注意所有這些樣式和用戶界面的改進(jìn)不需要寫一行代碼就可實(shí)現(xiàn)。我們只是在web控件的標(biāo)記中設(shè)定了一些屬性!事實(shí)上如果你正在使用類似visual studio .net的編輯器, 你可通過點(diǎn)擊一些按鈕、選中一些復(fù)選框、選擇列表框的一些項(xiàng)來設(shè)定格式化選項(xiàng)。想象一下在傳統(tǒng)asp中實(shí)現(xiàn)同樣效果需要編寫的那些冗長代碼,那會(huì)使你愛上asp.net,如果你現(xiàn)在還沒有的話。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 读书| 黄大仙区| 西吉县| 宝坻区| 汉寿县| 毕节市| 清水河县| 金秀| 林西县| 林州市| 吉木乃县| 霸州市| 南澳县| 安庆市| 桂林市| 阿克苏市| 镇坪县| 丁青县| 宁远县| 台州市| 镇原县| 海盐县| 眉山市| 手机| 奉贤区| 林州市| 鄂伦春自治旗| 怀安县| 西藏| 诸城市| 建平县| 新巴尔虎左旗| 建阳市| 和林格尔县| 鸡西市| 隆昌县| 甘泉县| 攀枝花市| 互助| 卓尼县| 古浪县|