Repeater.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater.aspx.cs" Inherits="Repeater" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<center>
<asp:Button ID="btnAddRecord" runat="server" Text="Add New Record" Width="600px"
Height="40px" Style="font-weight: 700; font-size: large;" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:Repeater runat="server" ID="Repeater1" OnItemCommand="OnItemCommand" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<asp:Table runat="server" ID="tblHead" BorderWidth="1" Width="600px" CellPadding="0"
CellSpacing="0" BackColor ="Blue" ForeColor="White">
<asp:TableRow runat="server" Font-Bold="true">
<asp:TableCell Width="150px" runat="server" BorderWidth="1"></asp:TableCell>
<asp:TableCell Width="150px" runat="server" BorderWidth="1">Dept ID</asp:TableCell>
<asp:TableCell Width="150px" runat="server" BorderWidth="1">Department</asp:TableCell>
<asp:TableCell Width="150px" runat="server" BorderWidth="1">Location</asp:TableCell>
</asp:TableRow>
</asp:Table>
</HeaderTemplate>
<ItemTemplate>
<asp:Table runat="server" ID="tblContent" BorderWidth="1" Width="600px" CellPadding="0" CellSpacing="0">
<asp:TableRow runat="server" ID="row" BorderWidth="1">
<asp:TableCell Width="150px" runat="server" BorderWidth="1">
<asp:LinkButton ID="Edit" runat="server" CommandName="edit">Edit</asp:LinkButton>
<asp:LinkButton ID="Delete" runat="server" CommandName="delete">Delete</asp:LinkButton>
</asp:TableCell>
<asp:TableCell Width="150px" runat="server" ID="tcID" BorderWidth="1">
<asp:Label ID="lblID" Text='<%# Bind("DeptId")%>' runat="server"></asp:Label>
<asp:PlaceHolder runat="server" ID="DIDEditPlaceholder" />
<input type="hidden" runat="server" id="DIDHidden" />
</asp:TableCell>
<asp:TableCell Width="150px" runat="server" ID="tcName" BorderWidth="1">
<asp:Label ID="lblDept" Text='<%# Bind("DName")%>' runat="server"></asp:Label>
<asp:PlaceHolder runat="server" ID="DNameEditPlaceholder" />
<input type="hidden" runat="server" id="DNameHidden" />
</asp:TableCell>
<asp:TableCell Width="150px" runat="server" ID="tcLoc" BorderWidth="1">
<asp:Label ID="lblLoc" Text='<%# Bind("DLocation")%>' runat="server"></asp:Label>
<asp:PlaceHolder runat="server" ID="DLocEditPlaceHolder" />
<input type="hidden" runat="server" id="DLocHidden" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="btnAddRecord"
PopupControlID="pnlPopUp">
</asp:ModalPopupExtender>
<asp:Panel ID="pnlPopUp" runat="server" Width="250px" BorderStyle="Groove" BackColor="AntiqueWhite">
<center>
<asp:Label ID="Label1" runat="server" Text="ID: "></asp:Label>
<asp:TextBox ID="txtID" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="Department: "></asp:Label>
<asp:TextBox ID="txtDepartment" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="Location: "></asp:Label>
<asp:TextBox ID="txtLocation" runat="server">
</asp:TextBox><br />
<asp:Button ID="btnAdd" runat="server" Text="Insert" OnClick="btnAdd_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
</center>
</asp:Panel>
</center>
</form>
</body>
</html>
Repeater.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.Data;
using System.Threading;
public partial class D2 : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=OrgDB;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = Bind();
Repeater1 .DataBind ();
//rotate.Visible = true;
}
public DataTable Bind()
{
SqlCommand cmd = new SqlCommand("Select DeptId, DName, DLocation from [tblDepartment]", conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
//Repeater1.DataSource = ds.Tables[0];
//Repeater1.DataBind();
}
protected void OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
conn.Open ();
SqlCommand cmd = new SqlCommand("Delete from tblDepartment where DeptId=@DID", conn);
cmd.Parameters.Add(new SqlParameter("@DID",((Label)e.Item.FindControl("lblID")).Text));
cmd.ExecuteNonQuery ();
conn.Close();
Repeater1.DataSource = Bind();
Repeater1.DataBind();
}
else if (e.CommandName == "edit")
{
// EditIndex = e.Item.ItemIndex;
//LinkButton btn = e.CommandSource as LinkButton; // Identify the link button
//RepeaterItem item = btn.NamingContainer as RepeaterItem;
//TableRow tr = e.Item.FindControl("row") as TableRow; // Identify the row and change its color
//tr.BackColor = System.Drawing.Color.ForestGreen;
ModalPopupExtender1.Show();
Label lblID = (Label)e.Item.FindControl("lblID");
Label lblDName = (Label)e.Item.FindControl("lblDept");
Label lblDLoc = (Label)e.Item.FindControl("lblLoc");
txtID.Text = lblID.Text;
txtDepartment.Text = lblDName.Text;
txtLocation.Text = lblDLoc.Text;
}
Repeater1.DataSource = Bind();
Repeater1.DataBind();
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
// Thread.Sleep(2000); //rotate.Visible = true;
// Thread.SpinWait(6);
string DeptName = txtDepartment.Text;
//string DName = ((TextBox)Repeater1.FindControl("NewDName")).Text;
string DLoc = txtLocation.Text;
// string DeptLoc = ((TextBox)Repeater1.FindControl("NewDLoc")).Text;
conn.Open();
SqlCommand cmd = new SqlCommand("Insert Into [tblDepartment](DName, DLocation) values(@DeptName, @DLoc)", conn);
cmd.Parameters.Add(new SqlParameter("@DeptName", DeptName));
cmd.Parameters.Add(new SqlParameter("@DLoc", DLoc));
cmd.ExecuteNonQuery();
conn.Close();
Repeater1.DataSource = Bind();
Repeater1.DataBind();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = new SqlCommand("Update tblDepartment set DName=@DeptName, DLocation=@DLoc where DeptId=@DID", conn);
cmd.Parameters.Add(new SqlParameter("@DID", txtID.Text));
cmd.Parameters.Add(new SqlParameter("@DeptName", txtDepartment.Text));
cmd.Parameters.Add(new SqlParameter("@DLoc", txtLocation.Text));
cmd.ExecuteNonQuery();
conn.Close();
Repeater1.DataSource = Bind();
Repeater1.DataBind();
}
}
No comments:
Post a Comment