Export (0) Print
Expand All

Data Binding Single-Value Web Server Controls at Design Time

Visual Studio .NET 2003

You can bind any single-value Web server control property — for example, the Text property of a TextBox or the ImageUrl property of an Image control — to a data value. This includes not only display properties, but also behavior properties such as size, width, and font.

Note   If you are working with list controls (Repeater, DataList, DataGrid, CheckBoxList, and RadioButtonList), you specify data binding for the control as a whole using special data-binding properties. For details, see Data Binding Multi-Record Web Server Controls. However, you can bind their single-value properties such as size and font using the procedure in this topic.

You specify single-value data binding by creating a data-binding expression. In Visual Studio, you can use design-time tools to create this expression automatically. When the control performs data binding, it resolves the expression and assigns the resulting value to the specified property. You can data bind as many properties on a control as you need.

Note   If you are working with HTML server controls (such as the HtmlInputText or HtmlSelect control), data-binding procedures are different. For details, see Data Binding HTML Server Controls.

To data bind single-value Web server controls in Design view

  1. Select the control, and in the Properties window, click the button in the (Data Bindings) property.

    The DataBindings dialog box is displayed, showing all the control's properties and data sources available at design time.

  2. Under Bindable Properties, select the property you want to bind to data.
  3. If you want to bind to a data source and member available at design time, select Simple Binding. Expand the appropriate node as follows:
    • If you are binding directly to data in a dataset, expand the name of the dataset and then the name of the table. This displays the DefaultView object (a data view automatically generated by the dataset), which in turn will expand into DefaultView.[0] — that is, to the first record in the data view.
      Note   To use this binding effectively, you must set the default data view's RowFilter property at run time so that the data view contains only one record. For details, see Adding Data Views to a Form or Component.
    • If you are adding controls to a template in a Repeater, DataList, or DataGrid control, expand the Container node, which references the data source for the parent control. Expand the DataItem node and then select the column to bind to. When the control runs, the DataItem will contain the record currently being processed by the parent container.
      Note   The objects available in the Simple Binding tree are these: the Page object; any data source the derives from the Component class; any objects instantiated in the page's InitializeComponent method; and any objects that implement the IEnumerable interface.

    When you select a data source and member, a data-binding expression is constructed and displayed in the disabled Custom binding expression box below.

  4. If you are working with a property that is of type String, and you want to format the results of the expression, select a formatting expression from the Format list.
  5. If you want to create your own data-binding expression or modify one generated by selections in the Simple binding, choose Custom binding expression.

    The editing box is enabled and you can type or edit the expression.

    Note   You can edit the binding expression in HTML view as well.
  6. Repeat Steps 2 through 5 for any other properties you want to bind.
  7. Click OK to close the dialog box.

    In the Properties window, a data-binding glyph (Aa713238.vbdatabindingglyph(en-us,VS.71).gif) appears in the property name box to indicate that the property is data bound.

  8. Add code to the Web Forms page to call the control's or the page's DataBind method.
    Note   You generally do not want to call the DataBind method on each round trip (that is, in the page initialization without checking for a post back), because doing so replaces the values in controls.

You can also specify data binding by editing the control declaration in HTML view of the Web Forms Designer.

To data bind single-value controls in HTML view

  1. Create a data-binding expression and assign it as the value for a property (attribute). The expression's syntax is:
    <%# expression %>
    

    The following example shows a data-binding expression. The expression gets the value of the au_lname column from the first record of a data view and assigns it to the Text property of a TextBox Web control.

    <asp:TextBox id="TextBox1" runat="server" 
       Text='<%# DataView1(0)("au_lname") %>'>
    </asp:TextBox>
    
  2. Add code to the Web Forms page to call the control's or the page's DataBind method.
    Note   You generally do not want to call the DataBind method on each round trip (that is, in the page initialization without checking for a post back), because doing so replaces the values in controls.

See Also

Introduction to Data Access in Web Forms Pages | Data Binding Expression Syntax | Multi-Record and Single-Value Data Binding for Web Forms Pages | Data Binding HTML Server Controls

Show:
© 2014 Microsoft