Controls You Can Use on Web Forms ASP.NET Standard Controls Substitution Control
Essentially, to create a working Substitution control, you need to add the control to the page and bind the control to a data source.
The below procedures show the minimum steps needed to display data in a Substitution control.
This example shows a Substitution control that is bound to an AccessDataSource control.
- Declare an <
asp:substitution
> element on the page. For syntax, see Substitution Control Syntax.
- Specify the control’s DataSourceID property.
- Optionally set the control’s base properties.
<asp:substitution id="plansView" runat="server"
datasourceid="plans"
width="90%"
cellpadding=5>
- Within the Substitution declaration, declare an <ItemTemplate> element.
- Within the ItemTemplate, define the HTML or server controls that will contain each field you intend to display.
To display the value of a field in an item template, use DataBinding
Expression Syntax.
<itemtemplate>
<div class="header">
<h3 style="color:maroon"><%# Eval ( "Model" ) %></h3>
</div>
<br>
...
<p>National Average Cost to Build: <br>
<b style="color:maroon"><%# Eval ( "Cost", "{0:c}" ) %></b>
...
</itemtemplate>
NOTE: The ItemTemplate must contain at least one HTML element or server control that binds to a field from the data source; otherwise the Substitution has nothing to render.
The DataSource property of a Substitution can also be set programmatically, just like any Web server control property. Below shows an example of binding a data source to a Substitution in code.
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e ) {
string query = "SELECT ProductType, ProductCode, ... FROM Products";
productSubstitution.DataSource = fetchReader ( query, "dbtutor" );
productSubstitution.DataBind ( );
}
</script>
The above examples apply several concepts and methods that are described elsewhere in this workshop. For particulars, see ADO.NET Primer, Introduction to Data Binding in Web Forms, Web Forms Server Controls Templates.
Introduction to the Substitution Control Specifying Row Format in a Substitution Control