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

首頁 > 開發 > 綜合 > 正文

DataGrid Web控件深度歷險(1)

2024-07-21 02:21:41
字體:
來源:轉載
供稿:網友
datagrid web控件深度歷險(1)



這篇文章是一系列關于使用datagrid web控件文章的第一部分。asp.net datagrid web控件可將數據庫信息顯示在html表格中,并且功能強大。在最簡單的情形下datagrid顯示html表格框架,但是它可被增強以顯示豐富的用戶界面,可根據數據庫的列進行排序,甚至允許對數據庫結果進行分頁!所有這些有趣的主題將在今后一系列文章中涉及。

從數據庫中獲取表格信息并將其顯示在一個html表格中是傳統asp編程中最普通的任務之一。在傳統asp編程中需要通過多行交織的html和代碼實現上述功能。下面的原形代碼顯示了這些代碼通常的形式。

create database connection
populate a recordset based on some sql query
output the html table header (<table ...>)
loop through the recordset
emit the html for a table row
...
emit the html table footer (</table>)


如果你是一個asp開發人員,你也許多次編寫了上述代碼。asp.net的優點之一就是它包含很多web控件。這些產生html的web控件提供了一個可編程的接口,它允許開發人員將代碼和內容分離,并在代碼中將產生html的實體作為對象使用。也就是說,如果我們需要通過asp.net顯示一些html內容,將編寫如下的代碼:

<script language="vb" runat="server">
sub page_load(sender as object, e as eventargs)
lblmessage.text = "hello, world!"
end sub
</script>

<asp:label runat="server" id="lblmessage" />

這里帶有runat=”server”屬性(類似于html標記)的lblmessage web控件被放置在html中。然后,在page_load事件處理程序中(該事件處理程序在每次頁面裝載時被調用)lblmessage的text屬性被設置為”hello world”。此處對于web控件的使用,實現了代碼和內容的分離。在傳統的asp中,需要將<%="hello, world!"%>放置在html中合適的位置才能達到同樣的效果。



datagrid基礎

要在asp.net web頁面中加入datagrid,只需執行如下代碼:

<asp:datagrid runat="server" id="id_of_datagrid" />
這里的id值將作為在服務器端代碼中使用datagrid的名稱,我們通過將上述語法放置在html中來使用datagrid。但是為了讓datagrid顯示任何有用的信息,我們需要將datagrid綁定到一些信息的集合。這些信息的集合可以是任何支持ienumerable接口的對象。它包括arrays,集合類(arraylist ,hashtable等),datasets和其它很多對象。由于希望集中精力顯示數據庫信息,因此在本文中我們僅關注將datagrid綁定至datareader。datareader類似于傳統ado/asp中順序的(forward-only)記錄集。(如需了解在ado.net中讀取數據庫結果至datareaders中,請閱讀efficiently iterating through results from a database query using ado.net )

那么如何將數據綁定至datagrid?其實出奇的簡單。第一件事是提取數據庫數據至datareader.對于本例,我使用aspfaqs.com數據庫,并且提取最受歡迎的10個問題。一旦將數據提取至datareader,將datareader綁定至datagrid只需兩行代碼。第一行將datagrid的datasource屬性設置為datareader;第二行調用datagrid的databind方法,代碼如下所示:



<% @import namespace="system.data" %>
<% @import namespace="system.data.sqlclient" %>
<script language="vb" runat="server">
sub page_load(sender as object, e as eventargs)
binddata()
end sub

sub binddata()
'1. create a connection
dim myconnection as new sqlconnection(
configurationsettings.appsettings("connectionstring"))

'2. create the command object, passing in the sql string
const strsql as string = "sp_popularity"
dim mycommand as new sqlcommand(strsql, myconnection)

'set the datagrid's datasource to the datareader and databind
myconnection.open()
dgpopularfaqs.datasource = mycommand.executereader(
commandbehavior.closeconnection)
dgpopularfaqs.databind()
end sub
</script>

<asp:datagrid id="dgpopularfaqs" runat="server" />

運行結果如下:

simple datagrid demo
this demo shows how to bind the results of a query to an unformatted datagrid.


faqid
description
viewcount
submittedbyname
submitted

byemail
date

entered
catname

144
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)?
161056
scott mitchell
[email protected]
3/20/2001 2:53:45 am
getting started

181
how can i format numbers and date/times using asp.net? for example, i want to format a number as a currency.
123888
scott mitchell
[email protected]
1/19/2002 3:12:07 pm
asp.net













首先注意用于編寫數據綁定的代碼數量不多。我們創建一個連接,指定一個sql命令(這里使用一個存儲過程,sp_popularity),打開數據庫連接,設定datagrid的datasource屬性為datareader,最后調用datagrid的databind方法。這種做法完全將代碼從內容分離,沒有像在傳統asp中混合html表格和datareader輸出的語法。

花些時間看一下運行結果。你會發現datagrid使用html表格顯示數據庫內容,盡管并不美觀。雖然我們完成了顯示數據這一主要工作,但用戶界面方面還有很多工作。幸運的是美化datagrid的結果出奇的簡單。遺憾的是需要等到下一篇文章中作介紹。



總結

這是一系列關于datagrid使用文章的一部分,我們研究了datagrid最基本的功能:熟悉asp.net web頁面和顯示綁定數據庫結果。遺憾的是datagrid的輸出并不美觀。但是我們不久會看到美化datagrid的結果很簡單。另外我們還將會在接下來的文章中看到更多用戶界面的高級選項,如數據庫結果的分頁顯示,datagrid結果的排序和其它功能。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汉阴县| 台北县| 莱西市| 和田县| 绥棱县| 佛学| 离岛区| 肃宁县| 义乌市| 定南县| 太谷县| 同心县| 夏邑县| 泉州市| 平昌县| 响水县| 河北省| 石台县| 盐城市| 安阳市| 揭西县| 互助| 合肥市| 清流县| 莱州市| 龙川县| 北海市| 文山县| 青海省| 罗江县| 延津县| 潮州市| 布尔津县| 黎平县| 上高县| 蒲城县| 灵武市| 贵南县| 山西省| 石狮市| 留坝县|