asp.net.ph

Skip Navigation Links

Adding Substitution Controls to a Web Forms Page

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.

To add a Substitution control to a Web Forms page

This example shows a Substitution control that is bound to an AccessDataSource control.

  1. Declare an <asp:substitution> element on the page. For syntax, see Substitution Control Syntax.
  2. Specify the control’s DataSourceID property.
  3. Optionally set the control’s base properties.
    <asp:substitution id="plansView" runat="server"
       datasourceid="plans"
       width="90%"
       cellpadding=5>
  4. Within the Substitution declaration, declare an <ItemTemplate> element.
  5. 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>
Substitution Example Details Page
Run Sample | View Source

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.

See Also

Introduction to the Substitution Control   Specifying Row Format in a Substitution Control



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note