ASP+ provides hierarchical databinding that allows page developers to declaratively associate databinding expressions between server control properties and data sources. Web Forms databinding allows you to bind a server control property to any public field or property on the page.
ASP+ databinding expressions are read-only.
Examples
The following example shows how a text box server control can be databound to a list of values contained in an ArrayList object:
<html> <script language="C#" runat=server> public void Page_Load(Object sender, EventArgs e) { ArrayList myList = new ArrayList(); myList.Add("DataItem One"); myList.Add("DataItem Two"); myList.Add("DataItem Three"); MyRepeater.DataSource = myList; MyRepeater.DataBind(); } </script> <body> <asp:repeater id="MyRepeater" runat=server> <template name="itemtemplate"> <input type=text value="<%#Container.DataItem%>" runat=server> </template> </asp:repeater> </body> </html>
In the code above, the Page_Load method creates and populates an ArrayList object named myList with three strings. The ArrayList object is assigned to be the datasource for the Repeater server control named MyRepeater:
MyRepeater.DataSource = myList;
The MyRepeater object's itemtemplate contains a textbox server control that will display the ArrayList values. The value attribute of the textbox is a databinding expression:
<input type=text value="<%#Container.DataItem%>" runat=server>
In the databinding expression, Container refers to the first parent control in the immediate control hierarchy that supports the INamingContainer Interface interface. In this case this is the RepeaterItem object. As RepeaterItem iterates through its ArrayList datasource, each value of the ArrayList is obtained by reference to the DataItem property.
Databinding Expression Evaluation and the DataBind Method
In the example above, the Page_Load method calls the DataBind method on the MyRepeater object after assigning its DataSource:
MyRepeater.DataSource = myList; MyRepeater.DataBind();
The page developer must explicitly call the DataBind method on a databound server control to invoke proper runtime evaluation of any databinding expressions contained in that control's child controls. In the example, the MyRepeater control has one child control, the textbox. In a more complex scenario, the databound server control might contain several child controls. Depending on the scenario, you may need to call the DataBind method more than once.