home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / aspplus / doc / webdatabinding.aspx < prev    next >
Encoding:
Text File  |  2000-06-08  |  10.6 KB  |  235 lines

  1.  
  2. <%@ Page Language="C#" %>
  3. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  4.  
  5. <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
  6.  
  7. <h4>Databinding Server Controls</h4>
  8. <p>
  9.  
  10. <div class="indent" style="font-family:Verdana; font-size:8pt;">
  11.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  12.     <a class="toc2" target="content" href="#intro">Databinding Overview and Syntax</a><br>
  13.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  14.     <a class="toc2" target="content" href="#properties">Binding to Simple Properties</a><br>
  15.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  16.     <a class="toc2" target="content" href="#lists">Binding to Collections & Lists</a><br>
  17.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  18.     <a class="toc2" target="content" href="#expressions">Binding Expressions or Methods</a><br>
  19.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  20.     <a class="toc2" target="content" href="#bindereval">DataBinder.Eval()</a><br>
  21.     <b> <img align="middle" src="/quickstart/images/bullet.gif">  </b>
  22.     <a class="toc2" target="content" href="#endofsection">Section Summary</a><br>
  23. </div>
  24. <p>
  25. <hr>
  26.  
  27. <!--BEGIN SECTION-->
  28. <a name="intro">
  29. <span class="subhead">Databinding Overview and Syntax</span>
  30. <p>ASP+ introduces a new declarative data binding syntax.  This extremely flexible syntax permits the developer 
  31. to bind not only to data sources, but also to simple properties, collections, expressions, and even results returned from 
  32. method calls.  Here are some examples:<p>
  33.  
  34. <div class="indent">
  35. <table class="table2" cellpadding="3">
  36. <tr>
  37.     <td style="width:15%"><b>Simple property</b></td>
  38.     <td class="code" style="color:black">Customer: <%# custID %></td>
  39. </tr><tr>
  40.     <td style="width:15%"><b>Collection</b></td>
  41.     <td class="code" style="color:black">Orders: <asp:ListBox id="List1" datasource='<%# myArray %>' runat="server"></td>
  42. </tr><tr>
  43.     <td style="width:15%"><b>Expression</b></td>
  44.     <td class="code" style="color:black">Contact: <%# ( customer.First Name + " " + customer.LastName ) %></td>
  45. </tr><tr>
  46.     <td style="width:15%"><b>Method result</b></td>
  47.     <td class="code" style="color:black">Outstanding Balance: <%# GetBalance(custID) %></td>
  48. </tr>
  49. </table>
  50. </div>
  51.  
  52. <p>Although this syntax looks similar to the ASP shortcut for Response.Write -- <span class="code"><%= %></span> -- its 
  53. behavior is quite different.  Whereas ASP's Response.Write shortcut syntax was evaluated when the page 
  54. was processed, the ASP+ data binding syntax is evaluated only when the <b>DataBind()</b> method is invoked.<p>
  55.  
  56. DataBind() is a method of the <b>Page</b> and all server controls.  When you call DataBind on a parent control it 
  57. is cascaded to all of the children of the control.  So, for example, <span class="code">DataList1.DataBind()</span> 
  58. invokes the DataBind() method on each of the controls in the DataList's templates.  Calling DataBind on the
  59. Page -- <span class="code">Page.DataBind()</span> or simply <span class="code">DataBind()</span> -- causes all 
  60. data binding expressions on the page to be evaluated.  DataBind() is commonly called from the Page_Load event:<p>
  61.  
  62. <div class="code"><xmp>
  63. protected void Page_Load(Object Src, EventArgs E) 
  64. {
  65.     DataBind();
  66. }
  67. </xmp>
  68. </div>
  69.  
  70. You can use a binding expression almost anywhere in the declarative section of an ASPX page, provided it evaluates to 
  71. the expected data type at runtime.  The simple property, expression and method examples above display text to 
  72. the user when evaluated.  In these cases, the data binding expression must evaluate to a value of type String.  
  73. In the collection example, the data binding expression evaluates to a value of valid type for the DataSource
  74. property of ListBox.  You may find it necessary to coerce the type of value in your binding expression to produce 
  75. the desired result.  For example, if <span class="code">count</span> is an integer:<p>
  76.  
  77. <div class="indent">
  78.         <span class="code">Number of Records:  <%# count.ToString() %></span>
  79. </div>
  80.  
  81. <!--BEGIN SECTION-->
  82. <br>
  83. <a name="properties">
  84. <br>
  85. <span class="subhead">Binding to Simple Properties</span>
  86. <p>The ASP+ data binding syntax supports binding to public variables, properties of the Page and 
  87. properties of other controls on the page.  
  88.  
  89. <p>This example illustrates binding to a public variable and simple property on the page.  An important thing 
  90. to note is that these values are initialized <b>before</b> DataBind() is called.   
  91.  
  92. <p>
  93. <!-- databinding to a prop on the page -->
  94. <Acme:SourceRef
  95.   RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind1.aspx"
  96.   ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind1.src"
  97.   Icon="/quickstart/aspplus/images/databind1.gif"
  98.   Caption="DataBind1.aspx"
  99.   runat="server" />
  100.   
  101. <p>This example illustrates binding to a property of another control.
  102. <p>
  103. <!-- databinding to a prop of another control -->
  104. <Acme:SourceRef
  105.   RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind2.aspx"
  106.   ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind2.src"
  107.   Icon="/quickstart/aspplus/images/databind2.gif"
  108.   Caption="DataBind2.aspx"
  109.   runat="server" />
  110.  
  111. <!--BEGIN SECTION-->
  112. <br>
  113. <a name="lists">
  114. <br>
  115. <span class="subhead">Binding to Collections & Lists</span>
  116. <p>List server controls like DataGrid, ListBox and HTMLSelect use a collection as a datasource.  The following 
  117. examples illustrate binding to common NGWS runtime collection types.  These controls can bind only to 
  118. collections that support the <b>ICollection</b> interface.  
  119. Most commonly, you'll bind to ArrayList, Hashtable, DataView and DataReader.
  120.  
  121. <p>
  122. <p>This example illustrates binding to an ArrayList.
  123. <p>
  124. <!-- DataBinding to an ArrayList -->
  125. <Acme:SourceRef
  126.   RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind3.aspx"
  127.   ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind3.src"
  128.   Icon="/quickstart/aspplus/images/databind3.gif"
  129.   Caption="DataBind3.aspx"
  130.   runat="server" />
  131.  
  132. <p>This example illustrates binding to a DataView.  Note that the DataView class is defined in the System.Data 
  133. namespace.
  134. <p>
  135. <!-- Databinding to a DataView -->
  136. <Acme:SourceRef
  137.   RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind4.aspx"
  138.   ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind4.src"
  139.   Icon="/quickstart/aspplus/images/databind4.gif"
  140.   Caption="DataBind4.aspx"
  141.   runat="server" />
  142.  
  143. <p>This example illustrates binding to a Hashtable.
  144. <p>
  145. <!-- Databinding to a Hashtable -->
  146. <Acme:SourceRef
  147.   RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind5.aspx"
  148.   ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind5.src"
  149.   Icon="/quickstart/aspplus/images/databind5.gif"
  150.   Caption="DataBind5.aspx"
  151.   runat="server" />
  152.  
  153. <!--BEGIN SECTION-->
  154. <br>
  155. <a name="expressions">
  156. <br>
  157. <span class="subhead">Binding Expressions or Methods</span>
  158. <p>Many times you'll want to manipulate data before binding to your page or a control.  This example illustrates 
  159. binding to an expression and the return value of a method.
  160.  
  161. <p>
  162. <!-- Databinding to the return value of a function -->
  163. <Acme:SourceRef
  164.   RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind7.aspx"
  165.   ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind7.src"
  166.   Icon="/quickstart/aspplus/images/databind6.gif"
  167.   Caption="DataBind7.aspx"
  168.   runat="server" />
  169.  
  170. <!--BEGIN SECTION-->
  171. <br>
  172. <a name="bindereval">
  173. <br>
  174. <span class="subhead">DataBinder.Eval()</span>
  175. <p>The ASP+ framework supplies a static method that evaluates late-bound data binding expressions and optionally 
  176. formats the result as a string.  DataBinder.Eval() is convenient in that it eliminates much of the explicit
  177. casting the developer must do to coerce values to the desired data type.  It is particularly useful when 
  178. data binding controls within a templated list, since often the both the data row and the data field 
  179. must be cast.  
  180.  
  181. <p>Consider the following example, where an integer will be displayed as a currency string.  With the
  182. standard ASP+ data binding syntax, you must first cast the type of the data row in order to retrieve the
  183. data field, IntegerValue.  Next, this is passed as an argument to the String.Format() method:<p>
  184.  
  185. <div class="indent">
  186.     <span class="code"><%# String.Format("{0:c}", ((DataRowView)Container.DataItem)["IntegerValue"]) %></span>
  187. </div>
  188.  
  189. <p>This syntax can be complex and difficult to remember.  In contrast, DataBinder.Eval() is simply a method with 
  190. three arguments: the naming container for the data item, the data field name, and a format string.  
  191. In a templated list like DataList, DataGrid or Repeater the naming container is always <span class="code">
  192. Container.DataItem.</span>  <b>Page</b> is another naming container that can be used with
  193. DataBinder.Eval().<p>
  194.  
  195. <div class="indent">
  196.     <span class="code"><%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %></span>
  197. </div>
  198.  
  199. <p>The format string argument is optional.  If it is omitted, DataBinder.Eval returns a value of type object.  For 
  200. example:<p>
  201.  
  202. <div class="indent">
  203.     <span class="code"><%# (bool)DataBinder.Eval(Container.DataItem, "BoolValue") %></span>
  204. </div>
  205.  
  206. <p>It's important to note that DataBinder.Eval() can carry a noticeable performance penalty over the 
  207. standard data binding syntax because it uses late-bound reflection.  Use judiciously, especially when when string 
  208. formatting is not required.
  209.  
  210. <p>
  211. <!-- Databinding in a templated list -->
  212. <Acme:SourceRef
  213.   RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind6.aspx"
  214.   ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind6.src"
  215.   Icon="/quickstart/aspplus/images/databind7.gif"
  216.   Caption="DataBind6.aspx"
  217.   runat="server" />
  218.  
  219. <p>
  220. <!--BEGIN SECTION-->
  221. <a name="endofsection">
  222.  
  223. <h4>Section Summary</h4>
  224. <ol>
  225. <li>The ASP+ declarative data binding syntax uses the <%# %> notation.
  226. <li>You can bind to data sources, properties of the page or another control, collections, expressions, and results 
  227. returned from method calls.
  228. <li>List controls can bind to collection which support the ICollection interface, such as ArrayList, Hashtable, 
  229. DataView and DataReader.
  230. <li>DataBinder.Eval() is a static method for late-binding.  It's syntax can be simpler than the standard data 
  231. binding syntax, but performance is slower.
  232. </ol>
  233. <p>
  234.  
  235. <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->