Merhaba arkadaşlar bu
makalemizde DetailsView da kayıt işlemi, kayıt güncelleme ve silme işlemlerinin
nasıl yapıldığını göreceğiz. DetailsView nesnesinde sayfalama için AllowPaging özelliğini AllowPaging="true" ve yine DetailsView nesnesinin
AutoGenerateRows özelliğini de AutoGenerateRows="false" yapın.
Screenshot
Şekil
1
Şekil
2
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindDetailsView();
}
}
private void bindDetailsView()
{
try
{
DataSet ds = GetDataSet("Select
* from Personel");
DetailsView1.DataSource = ds;
DetailsView1.DataBind();
}
catch (Exception ex) { throw ex; }
}
private DataSet GetDataSet(string Query)
{
DataSet ds = new DataSet();
try
{
string strCon = " Data
Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated
Security=True;Connect Timeout=30";
SqlConnection Con = new SqlConnection(strCon);
SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
Da.Fill(ds);
}
catch (Exception) { }
return ds;
}
private void ExecuteQuery(string Query)
{
try
{
string strCon = " Data
Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated
Security=True;Connect Timeout=30";
SqlConnection Con = new SqlConnection(strCon);
Con.Open();
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
Con.Close();
}
catch (Exception) { }
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs
e)
{
DetailsView1.PageIndex =
e.NewPageIndex;
bindDetailsView();
}
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
switch (e.CommandName.ToString())
{
case "Edit":
DetailsView1.ChangeMode(DetailsViewMode.Edit);
bindDetailsView();
break;
case "Cancel":
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
bindDetailsView();
break;
case "New":
DetailsView1.ChangeMode(DetailsViewMode.Insert);
bindDetailsView();
break;
}
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
DetailsView1.ChangeMode(e.NewMode);
bindDetailsView();
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
TextBox txtFirstname = (TextBox)DetailsView1.FindControl("txtFirstname");
TextBox txtLastName = (TextBox)DetailsView1.FindControl("txtLastName");
TextBox txtDepartrment = (TextBox)DetailsView1.FindControl("txtDepartment");
TextBox txtMail = (TextBox)DetailsView1.FindControl("txtMail");
Label lblIDEdit = (Label)DetailsView1.FindControl("lblIDEdit");
string Query = "Update
Personel Set Ad='" + txtFirstname.Text + "' ,Soyad ='" +
txtLastName.Text + "' ,Kısım ='" + txtDepartrment.Text + "',Mail='" + txtMail.Text + "'
where ID =" + lblIDEdit.Text;
ExecuteQuery(Query);
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
bindDetailsView();
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
TextBox txtFirstname = (TextBox)DetailsView1.FindControl("txtFirstname");
TextBox txtLastName = (TextBox)DetailsView1.FindControl("txtLastName");
TextBox txtDepartment = (TextBox)DetailsView1.FindControl("txtDepartment");
TextBox txtMail = (TextBox)DetailsView1.FindControl("txtMail");
DataSet ds = GetDataSet("Select
* from Personel");
int count = ds.Tables[0].Rows.Count +1;
string Query = "Insert into
Personel (id,Ad,Soyad,Kısım,Mail) values ('" +
count + "', '" + txtFirstname.Text + "','" + txtLastName.Text + "','" + txtDepartment.Text + "','" + txtMail.Text + "')";
ExecuteQuery(Query);
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
bindDetailsView();
}
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
Label lblID = (Label)DetailsView1.FindControl("lblID");
string Query = "Delete from
Personel where ID =" + lblID.Text;
ExecuteQuery(Query);
bindDetailsView();
}
public object ds { get; set; }
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.DetailsViewClass
{
font-family: verdana,arial,sans-serif;
font-size: 12px;
color: #333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
border-style: solid;
}
.Header
{
background: #b5cfd2;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
.Foter
{
background: #dcddc0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
.btn
{
background: #ffffff;
border-width: 1px;
padding: 2px;
border-style: solid;
border-color: #999999;
font-family: verdana,arial,sans-serif;
font-size: 11px;
}
.textbox
{
border-width: 1px;
padding: 1px;
border-style: solid;
border-color: #999999;
font-family: verdana,arial,sans-serif;
font-size: 11px;
width: 100px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="true" AutoGenerateRows="false" Height="50px" Width="125px" CssClass="DetailsViewClass" CellPadding="4" OnPageIndexChanging="DetailsView1_PageIndexChanging"
OnItemCommand="DetailsView1_ItemCommand" OnItemUpdating="DetailsView1_ItemUpdating"
OnModeChanging="DetailsView1_ModeChanging" OnItemInserting="DetailsView1_ItemInserting"
OnItemDeleting="DetailsView1_ItemDeleting">
<Fields>
<asp:TemplateField HeaderText="First
Name">
<ItemTemplate>
<asp:Label ID="lblID" Text='<%# Eval("ID") %>' Visible="false" runat="server"></asp:Label>
<asp:Label ID="lblFirstName" Text='<%# Eval("Ad") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblIDEdit" Text='<%# Eval("ID") %>' Visible="false" runat="server"></asp:Label>
<asp:TextBox ID="txtFirstname" Text='<%# Eval("Ad") %>' runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last
Name">
<ItemTemplate>
<asp:Label ID="lblLastName" Text='<%# Eval("Soyad") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" Text='<%# Eval("Soyad") %>' runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="lblDepartment" Text='<%# Eval("Kısım") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDepartment" Text='<%# Eval("Kısım") %>' runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mail">
<ItemTemplate>
<asp:Label ID="lblMail" Text='<%# Eval("Mail") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMail" Text='<%# Eval("Mail") %>' runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField Visible="true" ShowInsertButton="true" ShowCancelButton="true"
ShowDeleteButton="true" ShowEditButton="true" />
</Fields>
<PagerStyle CssClass="Foter" />
<FieldHeaderStyle Width="80px" CssClass="Header" />
</asp:DetailsView>
<br />
<br />
<br />
</div>
</form>
</body>
</html>
0 comments:
Yorum Gönder