home *** CD-ROM | disk | FTP | other *** search
-
- <%@ Page Language="C#" %>
- <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
-
- <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
-
- <h4>Databinding Server Controls</h4>
- <p>
-
- <div class="indent" style="font-family:Verdana; font-size:8pt;">
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#intro">Databinding Overview and Syntax</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#properties">Binding to Simple Properties</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#lists">Binding to Collections & Lists</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#expressions">Binding Expressions or Methods</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#bindereval">DataBinder.Eval()</a><br>
- <b> <img align="middle" src="/quickstart/images/bullet.gif"> </b>
- <a class="toc2" target="content" href="#endofsection">Section Summary</a><br>
- </div>
- <p>
- <hr>
-
- <!--BEGIN SECTION-->
- <a name="intro">
- <span class="subhead">Databinding Overview and Syntax</span>
- <p>ASP+ introduces a new declarative data binding syntax. This extremely flexible syntax permits the developer
- to bind not only to data sources, but also to simple properties, collections, expressions, and even results returned from
- method calls. Here are some examples:<p>
-
- <div class="indent">
- <table class="table2" cellpadding="3">
- <tr>
- <td style="width:15%"><b>Simple property</b></td>
- <td class="code" style="color:black">Customer: <%# custID %></td>
- </tr><tr>
- <td style="width:15%"><b>Collection</b></td>
- <td class="code" style="color:black">Orders: <asp:ListBox id="List1" datasource='<%# myArray %>' runat="server"></td>
- </tr><tr>
- <td style="width:15%"><b>Expression</b></td>
- <td class="code" style="color:black">Contact: <%# ( customer.First Name + " " + customer.LastName ) %></td>
- </tr><tr>
- <td style="width:15%"><b>Method result</b></td>
- <td class="code" style="color:black">Outstanding Balance: <%# GetBalance(custID) %></td>
- </tr>
- </table>
- </div>
-
- <p>Although this syntax looks similar to the ASP shortcut for Response.Write -- <span class="code"><%= %></span> -- its
- behavior is quite different. Whereas ASP's Response.Write shortcut syntax was evaluated when the page
- was processed, the ASP+ data binding syntax is evaluated only when the <b>DataBind()</b> method is invoked.<p>
-
- DataBind() is a method of the <b>Page</b> and all server controls. When you call DataBind on a parent control it
- is cascaded to all of the children of the control. So, for example, <span class="code">DataList1.DataBind()</span>
- invokes the DataBind() method on each of the controls in the DataList's templates. Calling DataBind on the
- Page -- <span class="code">Page.DataBind()</span> or simply <span class="code">DataBind()</span> -- causes all
- data binding expressions on the page to be evaluated. DataBind() is commonly called from the Page_Load event:<p>
-
- <div class="code"><xmp>
- protected void Page_Load(Object Src, EventArgs E)
- {
- DataBind();
- }
- </xmp>
- </div>
-
- You can use a binding expression almost anywhere in the declarative section of an ASPX page, provided it evaluates to
- the expected data type at runtime. The simple property, expression and method examples above display text to
- the user when evaluated. In these cases, the data binding expression must evaluate to a value of type String.
- In the collection example, the data binding expression evaluates to a value of valid type for the DataSource
- property of ListBox. You may find it necessary to coerce the type of value in your binding expression to produce
- the desired result. For example, if <span class="code">count</span> is an integer:<p>
-
- <div class="indent">
- <span class="code">Number of Records: <%# count.ToString() %></span>
- </div>
-
- <!--BEGIN SECTION-->
- <br>
- <a name="properties">
- <br>
- <span class="subhead">Binding to Simple Properties</span>
- <p>The ASP+ data binding syntax supports binding to public variables, properties of the Page and
- properties of other controls on the page.
-
- <p>This example illustrates binding to a public variable and simple property on the page. An important thing
- to note is that these values are initialized <b>before</b> DataBind() is called.
-
- <p>
- <!-- databinding to a prop on the page -->
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind1.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind1.src"
- Icon="/quickstart/aspplus/images/databind1.gif"
- Caption="DataBind1.aspx"
- runat="server" />
-
- <p>This example illustrates binding to a property of another control.
- <p>
- <!-- databinding to a prop of another control -->
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind2.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind2.src"
- Icon="/quickstart/aspplus/images/databind2.gif"
- Caption="DataBind2.aspx"
- runat="server" />
-
- <!--BEGIN SECTION-->
- <br>
- <a name="lists">
- <br>
- <span class="subhead">Binding to Collections & Lists</span>
- <p>List server controls like DataGrid, ListBox and HTMLSelect use a collection as a datasource. The following
- examples illustrate binding to common NGWS runtime collection types. These controls can bind only to
- collections that support the <b>ICollection</b> interface.
- Most commonly, you'll bind to ArrayList, Hashtable, DataView and DataReader.
-
- <p>
- <p>This example illustrates binding to an ArrayList.
- <p>
- <!-- DataBinding to an ArrayList -->
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind3.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind3.src"
- Icon="/quickstart/aspplus/images/databind3.gif"
- Caption="DataBind3.aspx"
- runat="server" />
-
- <p>This example illustrates binding to a DataView. Note that the DataView class is defined in the System.Data
- namespace.
- <p>
- <!-- Databinding to a DataView -->
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind4.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind4.src"
- Icon="/quickstart/aspplus/images/databind4.gif"
- Caption="DataBind4.aspx"
- runat="server" />
-
- <p>This example illustrates binding to a Hashtable.
- <p>
- <!-- Databinding to a Hashtable -->
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind5.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind5.src"
- Icon="/quickstart/aspplus/images/databind5.gif"
- Caption="DataBind5.aspx"
- runat="server" />
-
- <!--BEGIN SECTION-->
- <br>
- <a name="expressions">
- <br>
- <span class="subhead">Binding Expressions or Methods</span>
- <p>Many times you'll want to manipulate data before binding to your page or a control. This example illustrates
- binding to an expression and the return value of a method.
-
- <p>
- <!-- Databinding to the return value of a function -->
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind7.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind7.src"
- Icon="/quickstart/aspplus/images/databind6.gif"
- Caption="DataBind7.aspx"
- runat="server" />
-
- <!--BEGIN SECTION-->
- <br>
- <a name="bindereval">
- <br>
- <span class="subhead">DataBinder.Eval()</span>
- <p>The ASP+ framework supplies a static method that evaluates late-bound data binding expressions and optionally
- formats the result as a string. DataBinder.Eval() is convenient in that it eliminates much of the explicit
- casting the developer must do to coerce values to the desired data type. It is particularly useful when
- data binding controls within a templated list, since often the both the data row and the data field
- must be cast.
-
- <p>Consider the following example, where an integer will be displayed as a currency string. With the
- standard ASP+ data binding syntax, you must first cast the type of the data row in order to retrieve the
- data field, IntegerValue. Next, this is passed as an argument to the String.Format() method:<p>
-
- <div class="indent">
- <span class="code"><%# String.Format("{0:c}", ((DataRowView)Container.DataItem)["IntegerValue"]) %></span>
- </div>
-
- <p>This syntax can be complex and difficult to remember. In contrast, DataBinder.Eval() is simply a method with
- three arguments: the naming container for the data item, the data field name, and a format string.
- In a templated list like DataList, DataGrid or Repeater the naming container is always <span class="code">
- Container.DataItem.</span> <b>Page</b> is another naming container that can be used with
- DataBinder.Eval().<p>
-
- <div class="indent">
- <span class="code"><%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %></span>
- </div>
-
- <p>The format string argument is optional. If it is omitted, DataBinder.Eval returns a value of type object. For
- example:<p>
-
- <div class="indent">
- <span class="code"><%# (bool)DataBinder.Eval(Container.DataItem, "BoolValue") %></span>
- </div>
-
- <p>It's important to note that DataBinder.Eval() can carry a noticeable performance penalty over the
- standard data binding syntax because it uses late-bound reflection. Use judiciously, especially when when string
- formatting is not required.
-
- <p>
- <!-- Databinding in a templated list -->
- <Acme:SourceRef
- RunSample="/quickstart/aspplus/samples/webforms/DataBind/DataBind6.aspx"
- ViewSource="/quickstart/aspplus/samples/webforms/DataBind/DataBind6.src"
- Icon="/quickstart/aspplus/images/databind7.gif"
- Caption="DataBind6.aspx"
- runat="server" />
-
- <p>
- <!--BEGIN SECTION-->
- <a name="endofsection">
-
- <h4>Section Summary</h4>
- <ol>
- <li>The ASP+ declarative data binding syntax uses the <%# %> notation.
- <li>You can bind to data sources, properties of the page or another control, collections, expressions, and results
- returned from method calls.
- <li>List controls can bind to collection which support the ICollection interface, such as ArrayList, Hashtable,
- DataView and DataReader.
- <li>DataBinder.Eval() is a static method for late-binding. It's syntax can be simpler than the standard data
- binding syntax, but performance is slower.
- </ol>
- <p>
-
- <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->