DataList Tutorials- ASP.Net Using C# | Paras Babbar

DataList control ASP.Net using C#.

This post will help you get an idea about how to use Data List and its various features and command. This is an amazing control.

The Demo program will be available for download at the end of the post.

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="demo.aspx.cs" Inherits="demo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DataList ID="datalist" runat="server" RepeatColumns="2" OnEditCommand="datalist_EditCommand"
         OnCancelCommand="datalist_CancelCommand" OnUpdateCommand="datalist_UpdateCommand" OnDeleteCommand="datalist_DeleteCommand">
        <HeaderTemplate>
            <div style="width:800px; background-color:aqua; text-align:center; margin:0 auto;">
                Student Records
            </div>
        </HeaderTemplate>
        <ItemTemplate>
            <div style="background-color:rosybrown;">
                <table>
                    <tr>
                        <th>Roll No:</th>
                        <td><asp:Label ID="lbl_rl" runat="server" Text='<%#Eval("rollno") %>'></asp:Label></td>
                    </tr>
                    <tr>
                        <th>Name:</th>
                        <td><asp:Label ID="lbl_nm" runat="server" Text='<%#Eval("name") %>'></asp:Label></td>
                    </tr>
                    <tr>
                        <th>Course:</th>
                        <td><asp:Label ID="lbl_cr" runat="server" Text='<%#Eval("course") %>'></asp:Label></td>
                    </tr>
                    <tr>
                        <th><asp:Button ID="btn_edit" runat="server" Text="Edit" CommandName="edit" /></th>
                        <th><asp:Button ID="btn_delete" runat="server" Text="Delete" CommandName="delete" /></th>
                    </tr>
                </table>

            </div>
        </ItemTemplate>

        <EditItemTemplate>
            <div style="background-color:rosybrown;">
                <table>
                    <tr>
                        <th>Roll No:</th>
                        <td><asp:Label ID="elbl_rl" runat="server" Text='<%#Eval("rollno") %>'></asp:Label></td>
                    </tr>
                    <tr>
                        <th>Name:</th>
                        <td><asp:TextBox ID="etxt_nm" runat="server" Text='<%#Eval("name") %>'></asp:TextBox></td>
                    </tr>
                    <tr>
                        <th>Course:</th>
                        <td><asp:TextBox ID="etxt_cr" runat="server" Text='<%#Eval("course") %>'></asp:TextBox></td>
                    </tr>
                    <tr>
                        <th><asp:Button ID="btn_update" runat="server" Text="Update" CommandName="update" /></th>
                        <th><asp:Button ID="btn_cancel" runat="server" Text="Cancel" CommandName="cancel" /></th>
                    </tr>
                </table>
            </div>
        </EditItemTemplate>
        <FooterTemplate>
            <div style="width:800px; background-color:beige; text-align:center; margin:0 auto;">
                &copy;Paras Babbar
            </div>
        </FooterTemplate>
    </asp:DataList>
    </div>
    </form>
</body>
</html>


Default.aspx.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.SqlClient;
using System.Configuration;

public partial class demo : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
    string str = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            ShowData();
        }
    }

    private void ShowData()
    {
        //create connection
        con = new SqlConnection();
        con.ConnectionString = str;

        //create command
        cmd = new SqlCommand();
        cmd.CommandText = "Select * from student";

        //open connection
        cmd.Connection = con;
        con.Open();

        //execute command
        dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            datalist.DataSource = dr;
            datalist.DataBind();
        }
        else
        {
            Response.Write("<script>alert('No Data Found');</script>");
        }
    }
    protected void datalist_EditCommand(object source, DataListCommandEventArgs e)
    {
        datalist.EditItemIndex = e.Item.ItemIndex;
        ShowData();
    }
    protected void datalist_CancelCommand(object source, DataListCommandEventArgs e)
    {
        datalist.EditItemIndex = -1;
        ShowData();
    }
    protected void datalist_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        var roll = ((Label)e.Item.FindControl("elbl_rl")).Text;
        var name = ((TextBox)e.Item.FindControl("etxt_nm")).Text;
        var course = ((TextBox)e.Item.FindControl("etxt_cr")).Text;

        //create connection
        con = new SqlConnection();
        con.ConnectionString = str;

        //create command
        cmd = new SqlCommand();
        cmd.CommandText = "update student set name=@NM, course=@CR where rollno=@RL";
        cmd.Parameters.AddWithValue("@RL", roll);
        cmd.Parameters.AddWithValue("@NM", name);
        cmd.Parameters.AddWithValue("@CR", course);

        //open connection
        cmd.Connection = con;
        con.Open();

        //execute command
        int rowcount = cmd.ExecuteNonQuery();
        if (rowcount > 0)
        {
            datalist.EditItemIndex = -1;
            ShowData();
        }
        else
        {
            Response.Write("<script>alert('Error!');</script>");
        }

        //close connection
        con.Close();
    }
    protected void datalist_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        var rl = ((Label)e.Item.FindControl("lbl_rl")).Text;

        //create connection
        con = new SqlConnection();
        con.ConnectionString = str;

        //create command
        cmd = new SqlCommand();
        cmd.CommandText = "Delete from student where rollno=@RL";
        cmd.Parameters.AddWithValue("@RL", rl);

        //open connection
        cmd.Connection = con;
        con.Open();

        //execute command
        int rowcount = cmd.ExecuteNonQuery();
        if (rowcount > 0)
        {
            datalist.EditItemIndex = -1;
            ShowData();
        }
        else
        {
            Response.Write("<script>alert('Error!');</script>");
        }

        //close connection
        con.Close();
    }
}

 

Download Demo Work


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar