Asp.net programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this Asp.net and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Asp.net Website by URL, How to Play YouTube Videos in Asp.net by using Jquery , Jquery Change div background on mouseover in asp.net , JQuery UI Datepicker (Calendar) etc.

Thursday, July 3, 2014

How to use asp ListView control in asp.net:

How to use asp ListView control in asp.net:

Use asp List view control for display that in to page. As we know that for display data in the asp.net web application page is very easy task .for this .net gives much type of asp controls. As like Grid View Control, Repeater control, Data listView controls, ListView control etc.
In over past post we discuss
Asp.net ListView Related Post
  1.  how to bind data from the Grid view from data base,
  2.  How to bindvirtual table form grid view,
  3.  How to bind drop down list and others.
Now In this post we give some points for binding data form data base in List View.

List View:-

 "List view is a simple Asp control which is use for display data in to specific format ,some time the developer need to change the data presentation on page in own way so in this list view control it is very easy".

In this code we have a data table Post table in data base, in this table we consider id, name, post and created on fields and then in the .aspx page we take a List View Control and display name and post sequentially.  

How to use asp ListView control in asp.net:

How to use asp ListView control in asp.net:





code :


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="bind_list_view.aspx.cs" Inherits="bind_list_view" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">   
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ListView LayoutTemplate table - how to use table for ListView layout</title>   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="font-style:italic;">ListView Example: How To Use table layout in ListView control in ASP.Net</h2>
        <hr width="575" align="left"/>
        <asp:SqlDataSource
            ID="SqlDataSource1"
            runat="server"
            ConnectionString="<%$ ConnectionStrings:PastConnectionString %>"
            SelectCommand="SELECT [name], [post] FROM [Post_table]"
            >
        </asp:SqlDataSource>
        <br />
        <asp:ListView
            ID="ListView1"
            runat="server"
            DataSourceID="SqlDataSource1"
            >
            <LayoutTemplate>
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table ID="itemPlaceholderContainer" runat="server" border="1"
                               
                                style="width:500px; background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                                <tr runat="server" style="background-color: #E0FFFF;color: #333333;">
                                    <th runat="server">
                                        name</th>
                                    <th runat="server">
                                        post</th>
                                </tr>
                                <tr ID="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server"
                            style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <AlternatingItemTemplate>
                <tr style="background-color: #FFFFFF;color: #284775;">
                    <td>
                        <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
                    </td>
                    <td>
                        <asp:Label ID="postLabel" runat="server" Text='<%# Eval("post") %>' />
                    </td>
                </tr>
            </AlternatingItemTemplate>
            <EditItemTemplate>
                <tr style="background-color: #999999;">
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
                            Text="Update" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                            Text="Cancel" />
                    </td>
                    <td>
                        <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="postTextBox" runat="server" Text='<%# Bind("post") %>' />
                    </td>
                </tr>
            </EditItemTemplate>
            <EmptyDataTemplate>
                <table runat="server"
                    style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                    <tr>
                        <td>
                            No data was returned.</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert"
                            Text="Insert" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                            Text="Clear" />
                    </td>
                    <td>
                        <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="postTextBox" runat="server" Text='<%# Bind("post") %>' />
                    </td>
                </tr>
            </InsertItemTemplate>
            <ItemTemplate>
                <tr style="background-color: #E0FFFF;color: #333333;">
                    <td>
                        <asp:Label
                            ID="nameLabel"
                            runat="server"
                            Text='<%# Eval("name") %>'
                            ></asp:Label>
                    </td>
                    <td>
                        <asp:Label
                            ID="postLabel"
                            runat="server"
                            Text='<%# Eval("post") %>'
                            ></asp:Label>
                    </td>
                </tr>               
            </ItemTemplate>
            <SelectedItemTemplate>
                <tr style="background-color: #E2DED6;font-weight: bold;color: #333333;">
                    <td>
                        <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
                    </td>
                    <td>
                        <asp:Label ID="postLabel" runat="server" Text='<%# Eval("post") %>' />
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>
    </div>
    </form>
</body>

</html>

Related Post/Reference :

  1. ListView.LayoutTemplate Property
  2. How to use table for ListView layout in asp.net
  3. How to use table layout in ListView control in ASP.Net
  4. How to use asp  ListView control inasp.net
  5. ASP.NET ListView Demo - Data and layout templates