Como: BIND a dados em um controle modelo
The FormView, DataList, Repeater, and ListView Web server controls use templates to display data and to retrieve user input to insert, update, or delete data.In addition, you can use templates with the GridView and DetailsView controls to customize data layout.
You can bind a templated control to a data source control such as the LinqDataSource, ObjectDataSource or SqlDataSource control by setting the templated control's DataSourceID property to the ID of the data source control.You can then use the Eval and Bind functions within the template to bind to data from the data source.Para obter mais informações, consulte Sintaxe de expressões de ligação de dados.
Para acoplar um controle aos dados usando modelos
Add a data source control such as the SqlDataSource control to the page, as in the following example:
<asp:SqlDataSource ID="SqlDataSource1" SelectCommand="SELECT * FROM [Products]" ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" RunAt="server"> </asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" SelectCommand="SELECT * FROM [Products]" ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" RunAt="server"> </asp:SqlDataSource>
Add a control that supports templates, such as the ASP.NET FormView control.
Set the templated control's DataSourceID property to the ID of the data source control from Step 1, as in this example:
<asp:FormView ID="FormView1" DataSourceID="SqlDataSource1" DataKeyNames="ProductID" Runat="server"> </asp:FormView>
Adicione modelos ao controle modelado e preencha-o com controles e com marcações.
To display data, use the Eval function as a property setting and reference the bound data field.In templates used to insert or edit data, use the Bind function to reference the data bound field, as shown in the following example:
<asp:FormView ID="FormView1" DataSourceID="SqlDataSource1" DataKeyNames="ProductID" RunAt="server"> <ItemTemplate> <table> <tr><td align="right"><b>Product ID:</b></td> <td><%# Eval("ProductID") %></td></tr> <tr><td align="right"><b>Product Name:</b></td> <td><%# Eval("ProductName") %></td></tr> <tr><td align="right"><b>Category ID:</b></td> <td><%# Eval("CategoryID") %></td></tr> <tr><td align="right"><b>Quantity Per Unit:</b></td><td><%# Eval("QuantityPerUnit") %></td></tr> <tr><td align="right"><b>Unit Price:</b></td> <td><%# Eval("UnitPrice") %></td></tr> </table> </ItemTemplate> </asp:FormView>
<asp:FormView ID="FormView1" DataSourceID="SqlDataSource1" DataKeyNames="ProductID" RunAt="server"> <ItemTemplate> <table> <tr><td align="right"><b>Product ID:</b></td> <td><%# Eval("ProductID") %></td></tr> <tr><td align="right"><b>Product Name:</b></td> <td><%# Eval("ProductName") %></td></tr> <tr><td align="right"><b>Category ID:</b></td> <td><%# Eval("CategoryID") %></td></tr> <tr><td align="right"><b>Quantity Per Unit:</b></td><td><%# Eval("QuantityPerUnit") %></td></tr> <tr><td align="right"><b>Unit Price:</b></td> <td><%# Eval("UnitPrice") %></td></tr> </table> </ItemTemplate> </asp:FormView>
Cada controle do servidor da Web suporta modelos diferentes.For example, the Repeater control supports an ItemTemplate and an AlternatingItemTemplate to display data using alternating controls, styles, and markup.For details on Web server controls and supported templates, see Visão geral de controle servidor Web com dados vinculados.
Consulte também
Tarefas
Como: Conectar-se a um banco de dados ODBC usando o controle SqlDataSource