// data.aspx //
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="data.aspx.cs" Inherits="data" %>
<!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 runat="server">
<title></title>
<style type="text/css">
.btn
{
background-color: #033280;
color: White;
font-size: 12px;
font-weight: bold;
padding-left: 5px;
}
a
{
text-decoration: none;
font-weight: normal;
}
a:hover
{
text-decoration: underline;
}
.txt
{
font-size: 14px;
font-weight: bold;
padding-left: 5px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="500" cellpadding="0" cellspacing="0" align="center">
<tr>
<td colspan="2" height="40">
</td>
</tr>
<tr>
<td colspan="2" height="40">
<b>DataList Edit/Delete Operations</b>
</td>
</tr>
<tr>
<td colspan="2" height="40">
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" bgcolor="#FBF4E0">
<asp:DataList ID="dlData" runat="server" OnEditCommand="dlData_EditCommand" DataKeyField="eno"
OnCancelCommand="dlData_CancelCommand" OnDeleteCommand="dlData_DeleteCommand"
OnUpdateCommand="dlData_UpdateCommand">
<HeaderTemplate>
<table width="600" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30" class="btn">
Employee No.
</td>
<td class="btn">
Employee Name
</td>
<td class="btn">
Salary
</td>
<td colspan="2" class="btn" align="left">
Command
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td height="40" class="txt">
<%#Eval("id")%>
</td>
<td class="txt">
<%#Eval("name")%>
</td>
<td class="txt">
<%#Eval("salary")%>
</td>
<td class="txt">
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="edit" Text="Edit"></asp:LinkButton>
</td>
<td class="txt">
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="delete" Text="Delete"
OnClientClick="return confirm('Are you sure want delete this record?')"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td height="40" class="txt">
<%#Eval("id")%>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("name")%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Employee Name cannot be blank!"
ControlToValidate="TextBox2" Display="None"></asp:RequiredFieldValidator>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Text='<%#Eval("salary")%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Employee Salary cannot be blank!"
ControlToValidate="TextBox3" Display="None"></asp:RequiredFieldValidator>
</td>
<td class="txt">
<asp:LinkButton ID="lnkupdate" runat="server" CommandName="update" Text="Update"></asp:LinkButton>
</td>
<td class="txt">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="cancel" Text="Cancel"
CausesValidation="false"></asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
// data.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;
using System.Data.SqlClient;
public partial class data : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindList();
}
}
void bindList()
{
SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=asd;Integrated Security=True;");
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("select * from emp", sqlcon);
SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
dlData.DataSource = dt;
dlData.DataBind();
}
sqlcon.Close();
}
protected void dlData_EditCommand(object source, DataListCommandEventArgs e)
{
dlData.EditItemIndex = e.Item.ItemIndex;
bindList();
}
protected void dlData_UpdateCommand(object source, DataListCommandEventArgs e)
{
SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=asd;Integrated Security=True;");
string name = ((TextBox)e.Item.FindControl("TextBox2")).Text;
string salary = ((TextBox)e.Item.FindControl("TextBox3")).Text;
int id = Convert.ToInt32(dlData.DataKeys[e.Item.ItemIndex]);
SqlCommand sqlcmd = new SqlCommand("update emp set empname='" + name + "',salary='" + salary+ "' where id='" + id + "'", sqlcon);
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
dlData.EditItemIndex = -1;
bindList();
}
protected void dlData_CancelCommand(object source, DataListCommandEventArgs e)
{
dlData.EditItemIndex = -1;
bindList();
}
protected void dlData_DeleteCommand(object source, DataListCommandEventArgs e)
{
SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=asd;Integrated Security=True;");
int id = Convert.ToInt32(dlData.DataKeys[e.Item.ItemIndex]);
SqlCommand sqlcmd = new SqlCommand("delete from emp where id='" + id + "'", sqlcon);
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
bindList();
}
}
// SQL Table //
// OUTPUT //
No comments:
Post a Comment