【正文】
。這里可提供各種各樣的數(shù)據(jù)捆綁的選擇,包括: 捆綁數(shù)據(jù)到控件上和允許它決定讓它如何顯示,或者選擇在 頁(yè)中用申明的形式將數(shù)據(jù)綁定到控件上。 但同時(shí),當(dāng)時(shí)間和資源隨著系統(tǒng)調(diào)用元素項(xiàng)并決定調(diào)用返回?cái)?shù)據(jù)類型而慢慢耗盡時(shí),就必須謹(jǐn)慎的使用。它有如下句法: ( , “字段名 ”, “格式化選項(xiàng) ”) 使用這種句法結(jié)構(gòu),列表 D中看起來(lái)象 C代碼,就能像使用 第一個(gè)包含相同結(jié)構(gòu)的 。 當(dāng)在頁(yè)面中使用數(shù)據(jù)值時(shí), DataBinder類的 Eval方法能起著舉足輕重的作用。在 Visual Studio .NET 中, 這方法讓 RAD設(shè)計(jì)者很容易對(duì)數(shù)據(jù)捆綁的句法作語(yǔ)法分析。因此微軟對(duì) DataBinder類作了改進(jìn),使它的應(yīng)用變得相對(duì)要簡(jiǎn)單些。然而,這不是 的必須執(zhí)行的。 ItemIndex 屬性從零開(kāi)始, 因此一但有數(shù)值顯示,它就自動(dòng)的 +1 列表 B包含相等的 。 列表數(shù)值通過(guò)數(shù)據(jù)捆綁的技術(shù)被插入,而 Form 的頁(yè)面數(shù)值是通過(guò)使用方法調(diào)用來(lái)實(shí)現(xiàn)數(shù)值的傳遞。 它從 SQL 服務(wù)器 Northwind雇員表中選擇雇員名稱和電話號(hào)碼。 % MethodName( ) % 調(diào)用方法時(shí)返回的值顯示。% propertyName %的 runat=server 從頁(yè)面取回的數(shù)值 (數(shù)組,對(duì)象集 )綁定倒數(shù)據(jù)控件上。 如下則是提供了一些例子: % propertyName % 一頁(yè)面屬性值被返回。 % % % ( ( DataRowView ) )[PropertyName ] % % ( ( ObjectType ) ).PropertyName % % ( ( ObjectType ) ). ( ) % 句法是與當(dāng) 頁(yè)面的屬性和方法一致的被。 下列表達(dá)式則在原先的列基礎(chǔ)上每行再加上相應(yīng)的 C代碼。 % CStr ( ) % 用被改變的一數(shù)值覆蓋其串值表達(dá)式值。 % (“表達(dá) ”) % 從一個(gè) DataView容器的具體的值被返回。 下面的表達(dá)式是用 況,讓我們來(lái) 快速的回顧下。 DataItem 的實(shí)際的數(shù)據(jù)類型是由數(shù)據(jù)源確定的。 是專門為 DataItem 綁定到特殊項(xiàng)上專門設(shè)置的控件。 同樣地,相同的句法可以用在列表控件之外。 當(dāng)數(shù)據(jù)控件 (像轉(zhuǎn)發(fā)器一樣, DataGrid,如此等等 )綁定時(shí),表達(dá)參數(shù)通常是數(shù)據(jù)源的一張表的一個(gè)列名。 數(shù)據(jù)表達(dá)式可以放置在頁(yè)面的任何一處,或置于服務(wù)器控件成對(duì)標(biāo)簽的之間的附值處。 當(dāng)這些特性輕松的被使用時(shí),由于它是否被允許能夠被調(diào)用而經(jīng)常造成一些的混亂 。 數(shù)據(jù)捆綁的表達(dá)方式:就是能在 ASPX文件中以在此形式( %和 % )之間出現(xiàn)的代碼。如下 ,我們一起來(lái)談?wù)撍 page to simplify casting from one type to another. You can use the Eval method of the DataBinder class to make .NET do the heavy lifting when using data values in an page. The Eval method accepts the previously covered object。 runat=serverThe value retrieved from the page level property (array, collection of objects, etc.) is bound to the data control. % () %The value of the page level object property is displayed. % MethodName() %The value returned from the page method is displayed. You may use individual values (albeit properties, method return values, and so forth) on a page using the following syntax: %= Value % The C code in Listing A demonstrates data binding in an Web form. It selects employee names and telephone numbers from the SQL Server Northwind Employees table. The values from the query are displayed via an Repeater control. The column values are inserted via data binding, and the form39。re using C, the syntax is a bit different. The following list includes the corresponding C code for each line in the previous list. Notice the basic syntax is the same, but it changes when property values are returned and converted to the appropriate data type. % % % ((DataRowView))[PropertyName] % % ((ObjectType)).PropertyName % % ((ObjectType)).() % Syntax is consistent when working with page level properties and methods. The syntax remains the same as long as string values are returned. The following list provides some examples: % propertyName %The value for a page level property is returned. asp:ListBox id=lstValues datasource=39。s DataBind method is called. You can place data binding expressions on the value side of an attribute/value pair in the opening tag of a server control or anywhere in the page. All data binding expressions, regardless of where you place them, must be contained between % and % characters. When used with data controls (like Repeater, DataGrid, and so forth), the expression parameter is usually a column name from the data source. However, as long as it returns a value, any valid expression may be used. Likewise, the same syntax may be used outside list controls. This includes displaying values on the page or populating control attributes. is a runtime alias