Controls You Can Use on Web Forms ASP.NET Data Controls DetailsView Control
Essentially, to create a working DetailsView 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 DetailsView control.
This example shows a DetailsView control that is bound to a SqlDataSource control.
- Declare an <
asp:DetailsView
> element on the page. For syntax, see DetailsView Control Syntax.
- Specify the control’s DataSourceID property.
- Optionally set the control’s base properties.
<asp:detailsview id="customerDetails" runat="server"
datasourceid="customers"
width=300 cellpadding=3
font-size="9pt" />
By default, the DetailsView control will generate an entry for each field in the data source. Each field in the data source is rendered as a separate line, in the order it occurs in the data source. Field names appear in the column headers, and values are rendered in text labels.
The following demonstrates using a GridView control in combination with a DetailsView control to display master-detail information. When the user selects a customer from the GridView control, the customer’s detailed information is displayed in the DetailsView control.
Show me
The DataSource property of a DetailsView can also be set programmatically, just like any Web server control property. Below shows an example of binding a data source to a DetailsView in code.
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e ) {
string query = "SELECT ProductType, ProductCode, ... FROM Products";
productDetailsView.DataSource = fetchReader ( query, "dbtutor" );
productDetailsView.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 DetailsView Control Specifying Row Format in a DetailsView Control