Crud Operation Without Database (Using Grid View)
Design Coding-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Emp.aspx.cs" Inherits="Software.Emp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title></title>
<script>
$(function () {
$("#btnSave").click(function ()
{
var fname, mname, lname, dob, age;
{
fname = $("#txtFname").val();
mname = $("#txtMname").val();
lname = $("#txtLname").val();
dob = $("#dob").val();
age = $("#age").val();
if (fname == '' && mname == '' && lname == '' && dob == '' && age == '') {
alert("All field Mandatory");
return false;
}
if (fname == '')
{
alert("Enter First Name");
return false;
}
if (mname == '')
{
alert("Enter Middle Name");
return false;
}
if (lname == '')
{
alert("Enter Last Name");
return false;
}
if (dob == '')
{
alert("Enter Date Of Birth");
return false;
}
}
return true;
});
});
</script>
<style type="text/css">
.auto-style1 {
width: 45%;
height: 344px;
}
.auto-style2 {
text-align: right;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<center><div><h1>CRUD Operation Without Database</h1></div></center>
<table align="center" class="auto-style1">
<tr>
<td class="auto-style2">First Name:</td>
<td>
<asp:TextBox ID="txtFname" CssClass="form-control" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Middle Name:</td>
<td>
<asp:TextBox ID="txtMname" CssClass="form-control" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Last Name:</td>
<td>
<asp:TextBox ID="txtLname" CssClass="form-control" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Date of Birth:</td>
<td>
<asp:TextBox ID="dob" CssClass="form-control" runat="server"></asp:TextBox>
<asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton1_Click" Height="16px" Width="16px" />
<br />
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged" Visible="False" Height="134px"></asp:Calendar>
</td>
</tr>
<tr>
<td class="auto-style2">Age:</td>
<td>
<asp:Label ID="age" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnSave" class="btn-success" runat="server" Text="Submit" OnClick="btnSave_Click" />
<asp:Button ID="btnClear" CssClass="btn-danger" runat="server" Text="Clear" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridView1" Align="center" runat="server">
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace Software
{
public partial class Emp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if(Calendar1.Visible)
{
Calendar1.Visible = false;
}
else
{
Calendar1.Visible = true;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
dob.Text = Calendar1.SelectedDate.ToShortDateString();
var result = Calendar1.TodaysDate.Year - Calendar1.SelectedDate.Year;
age.Text = result.ToString();
Calendar1.Visible = true;
}
protected void btnSave_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("First Name");
dt.Columns.Add("Middle Name");
dt.Columns.Add("Last Name");
dt.Columns.Add("Date of Birth");
dt.Columns.Add("Age");
for(int row=0;row<GridView1.Rows.Count;row++)
{
DataRow dr = dt.NewRow();
dr[0]= GridView1.Rows[row].Cells[0].Text;
dr[1] = GridView1.Rows[row].Cells[1].Text;
dr[2] = GridView1.Rows[row].Cells[2].Text;
dr[3] = GridView1.Rows[row].Cells[3].Text;
dr[4] = GridView1.Rows[row].Cells[4].Text;
dt.Rows.Add(dr);
}
dt.Rows.Add(txtFname.Text, txtMname.Text, txtLname.Text, dob.Text, age.Text);
GridView1.DataSource = dt;
GridView1.DataBind();
txtFname.Text = string.Empty;
txtMname.Text = string.Empty;
txtLname.Text = string.Empty;
dob.Text = string.Empty;
age.Text = string.Empty;
}
}
}
----------------------------------------------------
No comments:
Post a Comment