Consuming Data with the DetailsView Control

Consuming Data with the DetailsView Control

The DetailsView control is an ASP.NET data-bound control that is used to display the details of a single record from a data source. It can be used to display read-only or editable data, and provides built-in support for paging, sorting, and editing.

To use the DetailsView control, you need to specify the data source and the fields to display in the control. Here’s an example of using the DetailsView control to display the details of a single record from the Northwind Customers table:

<asp:DetailsView ID=”CustomersDetailsView” runat=”server”

    AutoGenerateRows=”False” DataSourceID=”CustomersDataSource”>

    <Fields>

        <asp:BoundField DataField=”CustomerID” HeaderText=”Customer ID” ReadOnly=”True” />

        <asp:BoundField DataField=”CompanyName” HeaderText=”Company Name” />

        <asp:BoundField DataField=”ContactName” HeaderText=”Contact Name” />

        <asp:BoundField DataField=”ContactTitle” HeaderText=”Contact Title” />

        <asp:BoundField DataField=”Address” HeaderText=”Address” />

        <asp:BoundField DataField=”City” HeaderText=”City” />

        <asp:BoundField DataField=”Region” HeaderText=”Region” />

        <asp:BoundField DataField=”PostalCode” HeaderText=”Postal Code” />

        <asp:BoundField DataField=”Country” HeaderText=”Country” />

        <asp:BoundField DataField=”Phone” HeaderText=”Phone” />

        <asp:BoundField DataField=”Fax” HeaderText=”Fax” />

    </Fields>

</asp:DetailsView>

<asp:SqlDataSource ID=”CustomersDataSource” runat=”server”

    ConnectionString=”<%$ ConnectionStrings:NorthwindConnectionString %>”

    SelectCommand=”SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE CustomerID = @CustomerID”>

    <SelectParameters>

        <asp:QueryStringParameter Name=”CustomerID” QueryStringField=”id” Type=”String” />

    </SelectParameters>

</asp:SqlDataSource>

In this example, the DetailsView control is bound to the CustomersDataSource SqlDataSource control, which retrieves the data from the Customers table in the Northwind database. The SelectCommand of the SqlDataSource control specifies a parameterized SQL query that selects a single record based on the value of the “id” query string parameter. The Fields collection of the DetailsView control specifies the fields to display in the control, and each BoundField specifies the data field to bind to, as well as the header text to display for that field.

To display the details of a specific record, you can pass the ID value as a query string parameter in the URL of the page that contains the DetailsView control, like this:

http://localhost/MyPage.aspx?id=ALFKI In this example, the value “ALFKI” is passed as the value of the “id” query string parameter, which is used in the SelectCommand of the SqlDataSource control to retrieve the corresponding record from the Customers table.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Get industry recognized certification – Contact us

Menu