10 February 2013

DataList Example Using RepeatColumns,HeaderTemplate & ItemTemplate.

DataList
Create Database:-
        create database student
Create Table:-   
        create table student_details(Id int identity(1000,1) primary key,S_Name varchar(200),Course varchar(100),Branch varchar(100),Cell varchar(200),DOB varchar(200),DOJ varchar(200),Email varchar(200),Address nvarchar(max),Photo varchar(100)). 
Insert Data:-
         insert into student_details(S_Name,Course,Branch,Cell,DOB,DOJ,Email,Address,Photo)values('jc.adi','.net','cse','9494543460','24/04/1991','1/1/2012','jc.adi101@gmail.com','Tadipatri,a.p','Image0007.jpg').
Output:-
Asp.net Code:-

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="datalist.aspx.cs" Inherits="gridview_datalist" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    DataList Example:
    <h2 style="color: Green">
        DataList in ASP.NET using C#</h2>
    <asp:DataList ID="DataListExample" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"
        BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Font-Bold="True"
        Font-Names="Verdana" Font-Size="Small" GridLines="Both" RepeatColumns="2" RepeatDirection="Horizontal"
        Width="463px">
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" Font-Size="Large" ForeColor="White"
            HorizontalAlign="Center" VerticalAlign="Middle" />
        <HeaderTemplate>
            Student Information
        </HeaderTemplate>
        <ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        Id:
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>' ForeColor="#000099" />
                        <br />
                        Name:
                        <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("S_Name") %>' ForeColor="#000099" />
                        <br />
                        Course:
                        <asp:Label ID="DOBLabel" runat="server" Text='<%# Eval("Course") %>' />
                        <br />
                        Branch:
                        <asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Branch") %>' />
                        <br />
                        Cell:
                        <asp:Label ID="CourseLabel" runat="server" Text='<%# Eval("Cell") %>' />
                        <br />
                        DOB:
                        <asp:Label ID="BranchLabel" runat="server" Text='<%# Eval("DOB") %>' />
                        <br />
                        DOJ:
                        <asp:Label ID="CityLabel" runat="server" Text='<%# Eval("DOJ") %>' />
                        <br />
                        Email:
                        <asp:Label ID="MobileNoLabel" runat="server" Text='<%# Eval("Email") %>' />
                        <br />
                        Address:
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Address") %>' />
                    </td>
                    <td>
                        <asp:Image ID="img1" runat="server" Height="60px" Width="60px" ImageUrl='<%# "~/Datalist/images/"+ Eval("Photo") %>' />
                    </td>
                </tr>
            </table>
        </ItemTemplate>
        <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    </asp:DataList>
</asp:Content>


C#.net Code:- 

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 gridview_datalist : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["student"].ToString());
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        string sql = "select * from student_details";
        con.Open();
        cmd = new SqlCommand(sql, con);
        da.SelectCommand = cmd;
        da.Fill(ds);
        DataListExample.DataSource = ds;
        DataListExample.DataBind();
    }
}
Output Screens:-

1.
        
 
 
2.