The list view displays the columns and rows of control data and allows trimmings and paging. This is by far the most popular data display control, and it is ideal to understand how data performance control data retrieval control and how to interact with the code.
Note: For interaction with databases like Create, Read, Update and Delete, you must have prior knowledge of SQL
I will discuss the following procedures for binding the ListView control:
- SQL Server Express
- Microsoft sql server
Listview.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListView.aspx.cs" Inherits="ListView"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head2" runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:BlogEngineConnectionString %>"
SelectCommand="SELECT * FROM [Comments]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListView.aspx.cs" Inherits="ListView"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ListView Control - ASP.NET Tutorials</title>
<style type="text/css">
.TableCSS
{
border-style:none;
background-color:#3BA0D8;
width: 850px;
}
.TableHeader
{
background-color:#66CCFF;
color:#0066FF;
font-size:large;
font-family:Verdana;
}
.TableData
{
background-color:#82C13E;
color:#fff;
font-family:Courier New;
font-size:medium;
font-weight:bold;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:#3BA0D8; font-style:italic;">ListView Control Example in ASP.NET: How To Use ListView Control</h2>
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<LayoutTemplate>
<table id="Table1" runat="server" class="TableCSS">
<tr id="Tr1" runat="server" class="TableHeader">
<td id="Td1" runat="server">Comment ID</td>
<td id="Td2" runat="server">Blog ID</td>
<td id="Td3" runat="server">Date</td>
<td id="Td4" runat="server">Name</td>
<td id="Td5" runat="server">Comments</td>
</tr>
<tr id="ItemPlaceholder" runat="server">
</tr>
<tr id="Tr2" runat="server">
<td id="Td6" runat="server" colspan="2">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ButtonType="Link" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class="TableData">
<td>
<asp:Label
ID="Label1"
runat="server"
Text='<%# Eval("id")%>'
>
</asp:Label>
</td>
<td>
<asp:Label
ID="Label2"
runat="server"
Text='<%# Eval("Blog_id")%>'>
</asp:Label>
</td>
<td>
<asp:Label
ID="Label3"
runat="server"
Text='<%# Eval("Date")%>'>
</asp:Label>
</td>
<td>
<asp:Label
ID="Label4"
runat="server"
Text='<%# Eval("Name")%>'>
</asp:Label>
</td>
<td>
<asp:Label
ID="Label5"
runat="server"
Text='<%# Eval("Comment")%>'>
</asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:BlogEngineConnectionString %>" SelectCommand="SELECT * FROM [Comments]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
Listview.asp.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=(local);database=BlogEngine;Integrated Security=true;");
SqlDataAdapter da = new SqlDataAdapter("select * from Comments", con);
DataSet ds = new DataSet();
da.Fill(ds, "FillComent");
ListView1.DataSource = ds.Tables["FillComent "];
ListView1.DataBind();
}
}
0 comments:
Post a Comment