Setting up the ListView

Setting up the ListView

To set up a ListView control in ASP.NET, follow these steps:

In your ASP.NET web application, open the page where you want to use the ListView control.

From the “Data” section of the Toolbox, drag a ListView control onto the page.

In the Properties window, set the “DataSourceID” property to the ID of the LinqDataSource control that you want to use to retrieve data for the ListView.

In the markup for the ListView, define the layout and appearance of the control using the “LayoutTemplate”, “ItemTemplate”, “AlternatingItemTemplate”, “EditItemTemplate”, and “InsertItemTemplate” properties. For example, to display the “CustomerID” and “ContactName” columns in the ListView, you could use the following markup:

<asp:ListView runat=”server” ID=”lvCustomers”>

<LayoutTemplate>

<table>

<tr>

<th>Customer ID</th>

<th>Contact Name</th>

</tr>

<asp:PlaceHolder runat=”server” ID=”itemPlaceholder” />

</table>

</LayoutTemplate>

<ItemTemplate>

<tr>

<td><%# Eval(“CustomerID”) %></td>

<td><%# Eval(“ContactName”) %></td>

</tr>

</ItemTemplate>

</asp:ListView>

This markup defines a table with headers for the “Customer ID” and “Contact Name” columns, and an “ItemTemplate” that displays the corresponding data for each record retrieved from the LinqDataSource.

Optionally, you can define other templates such as the “EditItemTemplate” or “InsertItemTemplate” to enable editing or inserting records.

To customize the behavior of the ListView, you can handle events such as “SelectedIndexChanged” or “ItemCommand” in the code-behind file. For example, to handle the “SelectedIndexChanged” event and display the selected record in a DetailsView control, you could use the following code:

protected void lvCustomers_SelectedIndexChanged(object sender, EventArgs e)

{

int customerId = (int)lvCustomers.SelectedDataKey.Value;

dvCustomerDetails.ChangeMode(DetailsViewMode.ReadOnly);

dvCustomerDetails.DataSourceID = “ldsCustomerDetails”;

dvCustomerDetails.PageIndex = 0;

dvCustomerDetails.DataBind();

} This code retrieves the value of the “CustomerID” column for the selected record, sets the DetailsView control to read-only mode, and binds it to a LinqDataSource control that retrieves additional details for the selected customer.

Apply for ASP.NET Certification Now!!

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

Back to Tutorial

Share this post
[social_warfare]
Session creation in PHP
Deleting sessions and variables

Get industry recognized certification – Contact us

keyboard_arrow_up