Yazılımcının Günlüğü | YazilimGunlugu.Com
Arama
Makale Kategorileri
Üye Girişi
Şuanki online üyeler()
Aktif üye bulunmamaktadır.


ASP.NET 2.0 FormView Kontrolü

Yazar: Hasan Mansur

Kategori: ASP.NET

Eklenme Tarihi: 22.05.2007 01:55:16

Merhaba ; bu makalemde ASP.NET 2.0 ile beraber gelen ve DetailsView ya da ASP.NET 1.1 deki DataList kontrolleri gibi kayitları tek tek listelemek ve kayitlarda işlem yapmamiza olanak saglayan FormView kontrolünü inceleyeceğiz.Ancak makalemdeki örnek için verileri veritabanından almak ve orada tutmak

ASP.NET 2.0 FormView Kontrolü

FormView kontrolü ; giriş kisminda da anlattigim gibi kayitlari tek tek listelemek ve üzerlerinde Insert, Update , Delete , Select (Navigation) işlemlerini yapmamiza olanak saglamaktadir. Bunu yaparken row field ‘lar kullanmak yerine user defined template seçenekleri sunmaktadir. U template’ ler sayesinde kontrole ait tüm özellikleri kisisellestirerek verinin görünümünü ve sunumunu kolaylastirmaktadir. GridView , DetailsView ve FormView mantikl olarak ayni isi yapmakta olduklarindan dolayi hangi kontrolü kullanmaniz gerektigine karar vermek için bu üç kontrolü iyice anlamamiz gerekmektedir.

DetailsView ile FormView arasindaki en temel fark, DetailsView sonuçlari listelerken belli bir tablo düzeni kullanirken FormView ise kullanıcının tanimlayacagi herhangi bir template kullanarak listelenecek görünümg belirler.

FormView hiyerarsisini asagidaki sekilde görebilirsiniz :

Hyrarchy.JPG

Sekil 1 : FormView kontrolünün hiyerarsisi

Bos bir Web Site açalim ve formun içine TollBox’ tan bir adet FormView , ObjectDataSource ve Button alalim.

1.JPG

Projemize bir adet Class eklememiz gerekiyor. Bu calss içerisinde , örnegimizde kullanacagimiz verileri tanimlamamiz gerekiyor yani Customers tablosunu class içerisinde tanimlayacagiz.Bunu için gerekli property ve metodlari tanimlayacagiz.

Customers CLASS :

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Collections.Generic;

public class Customers

{

private int customerID;

private string customerName;

private string customerSurname;

private int customerAge;

private char customerSexual;

public Customers() {}

public Customers(int customerID,string customerName, string customerSurname, int customerAge, char customerSexual)

{

CustomerID = customerID;

Name = customerName;

Surname = customerSurname;

Age = customerAge;

Sexual = customerSexual;

}

public int CustomerID

{

get { return customerID; }

set { customerID = value; }

}

public string Name

{

get { return customerName; }

set

{

if (value != null || value !=String.Empty)

customerName = value;

else

customerName = "NoName";

}

}

public string Surname

{

get { return customerSurname; }

set

{

if (value != null || value != String.Empty)

customerSurname = value;

else

customerSurname = "NoSurame";

}

}

public int Age

{

get { return customerAge; }

set

{

if (value != 0)

customerAge = value;

else

customerAge = -1;

}

}

public char Sexual

{

get { return customerSexual; }

set { customerSexual = value; }

}

public static List<Customers> GetCustomerList()

{

List<Customers> custList = new List<Customers>();

custList.Add(new Customers(1,"Hasan","Mansur",27,'M'));

custList.Add(new Customers(2,"Figen","Mansur",27,'F'));

custList.Add(new Customers(3,"Utku", "Koç", 27, 'M'));

custList.Add(new Customers(4,"Filiz", "Koç", 28, 'F'));

custList.Add(new Customers(5,"Songül", "Mansur", 24, 'F'));

custList.Add(new Customers(6,"Onur", "Mansur", 4, 'M'));

custList.Add(new Customers(7,"Anil", "Mansur", 2, 'M'));

return custList;

}

}

Simdi de bu listeye yani collection’ a yapilacak işlemleri tanimlayacak bir class daha yazmamiz gerekiyor.Bu işlemler Select , Update , Insert ve Delete işlemleridir.

CustomerList.cs

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Collections.Generic;

public class CustomerList

{

private static List<Customers> customerList;

public static void Initialize()

{

customerList = Customers.GetCustomerList();

}

public List<Customers> Select()

{

return customerList;

}

public void Update(Customers updatedCustomer)

{

Customers foundCustomer = customerList.Find(delegate(Customers cust) { return cust.CustomerID == updatedCustomer.CustomerID; });

foundCustomer.Name = updatedCustomer.Name;

foundCustomer.Surname = updatedCustomer.Surname;

foundCustomer.Age = updatedCustomer.Age;

foundCustomer.Sexual = updatedCustomer.Sexual;

}

public void Insert(Customers insertCustomer)

{

customerList.Add(insertCustomer);

}

public void Delete(Customers deleteCustomer)

{

Customers foundCustomer = customerList.Find(delegate(Customers cust) { return cust.CustomerID == deleteCustomer.CustomerID; });

customerList.Remove(foundCustomer);

}

public int Count()

{

return customerList.Count;

}

public CustomerList()

{}

}

Bu işlemleri tamamladiktan sonra artik formumuzda bulunan ObjectDataSource’ un Properties penceresinden yaralanarak yapacagi işlemleri tanimlamamiz gerekmektedir.

2.JPG

Bu işlemi de taammladiktan sonra artik FormView kontrolünün ayarlamalarini yapmamiz kaliyor. FormView deki Template’ leri tanimlayalim öncelikle :

· ItemTemplate – FormView de verilerin nasıl görüntülenecegini belirleyecegimiz template

· HeaderTemplate – Kisisel bir baslik eklemek için kullanilan template

· FooterTemplate – FormView altinda kisisel bir yazi eklemek için kullanilan template

· EmptyDataTemplate –FormView ‘in DataSource’ u bos gelirse yani kayit dönmezse , EmptyDataTemplate ItemTemplate tarafinda kullanilir

· PagerTemplate – Sayfalama kontrolü için kullanilan template

· EditItemTemplate / InsertItemTemplate – Edit ve Insert işlemleri için kullanilan template

FormView kontrolüne sag tiklayarak “Edit Template” à “ItemTemplate” seçelim. Burada bir tablo yaratacaksiniz ve de sekildeki görünümü manual olarak yaratarak Item Template kismini sonlandiracagiz.Böylece kayitlar listelenirken , burada belirttigimiz düzende listeleneceklerdir.

ItemTemplate.JPG

Burasini bitirdikten sonra sag tiklayarak “End Template Editing” diyerek ItemTemplate’ i tamamliyoruz.Simdi de yine FormView’e sag tiklayarak “Edit Template” à “EditItemTemplate” seçelim ve asagidaki gibi ayarlayalim.Edit Item Template , kullanici herhangi bir kayitta iken “Edit” e tiklaninca FormView’ in alacagi görüntüyü ayarlar.

EditTemplate.JPG

Burasini da bitirdikten sonra sag tiklayarak “End Template Editing” diyerek ItemTemplate’ i tamamliyoruz.Simdi de yine FormView’e sag tiklayarak “Edit Template” à “InsertItemTemplate” seçelim ve asagidaki gibi ayarlayalim.Insert Item Template , kullanici “New” e tiklayinca yeni bir kayit girmesi için gerekli kontrolleri olusturacaktir

InsertTemplate.JPG.

Bu işlemler bittikten sonra FormView kontrolümüzün görünümlerini tamamlamis oluyorsunuz.FormView e sag tiklayarak “Auto Format” seçenegi ile kontrolümüze hazir bazi görünüm formatlarini uygulayabiliriz.

FormView_Preview.JPG

Bunlari yaptiktan sonra HTML source tarafi su sekilde olacaktir.

ASPX HTML Source Kod :

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

<!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>FormView DEMO</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:ObjectDataSource ID="ObjectDataSourceCustomers" runat="server"

DataObjectTypeName="Customers" TypeName="CustomerList"

SelectMethod="Select" UpdateMethod="Update"

DeleteMethod="Delete" InsertMethod="Insert">

</asp:ObjectDataSource>

&nbsp;&nbsp;<br />

<asp:Button ID="btnCustomers" runat="server" OnClick="BtnCustomers_Click" Text="Get Customers List" /><br />

<br /> &nbsp;</div>

<div>

<asp:FormView ID="FormView1" runat="server" BackColor="White" BorderColor="#999999"

BorderStyle="Solid" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="30%" ForeColor="Black">

<FooterStyle BackColor="#CCCCCC" />

<EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />

<ItemTemplate>

<table width="100%">

<tr >

<td align="center"><hr />

<table width="100%">

<tr>

<td valign="top" align=right >

<span style="font-weight: bold; color: blue">CustomerID:</span>

</td>

<td valign=top align=left >

<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>'> </asp:Label>

</td>

</tr>

<tr>

<td valign="top" align=right>

<span style="font-weight: bold; color: blue">Customer Name :</span>

</td>

<td valign="top" align=left >

<asp:Label ID="CustomerNameLabel" runat="server" Text='<%# Eval("Name") %>'> </asp:Label>

</td>

</tr>

<tr>

<td valign="top" align=right >

<span style="font-weight: bold; color: blue">Customer Surname:</span>

</td>

<td valign="top" align=left >

<asp:Label ID="CustomerSurnameLabel" runat="server" Text='<%# Eval("Surname") %>'> </asp:Label>

</td>

</tr>

<tr>

<td valign="top" align=right >

<span style="font-weight: bold; color: blue">Customer Age:</span>

</td>

<td valign="top" align=left style="width: 219px">

<asp:Label ID="CustomerAgeLabel" runat="server" Text='<%# Eval("Age") %>'> </asp:Label>

</td>

</tr>

<tr>

<td valign="top" align=right>

<span style="font-weight: bold; color: blue">Customer Sexual:</span>

</td>

<td valign="top" align=left>

<asp:Label ID="CustomerSexualLabel" runat="server" Text='<%# Eval("Sexual") %>'> </asp:Label>

</td>

</tr>

<tr>

<td valign="top" align=left style="width: 143px; text-align: center">

<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Edit"

Font-Bold="True">Edit</asp:LinkButton>

</td>

<td valign="top" align=center >

<asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False" CommandName="New"

Font-Bold="True">New</asp:LinkButton>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

<asp:LinkButton ID="LinkButton5" runat="server" CausesValidation="False" CommandName="Delete"

Font-Bold="True">Delete</asp:LinkButton></td>

<td valign="top" align=right>

&nbsp;</td>

</tr>

</table>

</td></tr>

</table>

</ItemTemplate>

<EditItemTemplate>

<table width="100%">

<tr>

<td align="center"><hr />

<table width="100%">

<tr>

<td valign="top" align=right width="100%">

<span style="font-weight: bold; color: blue">CustomerID :</span>

</td>

<td valign="top" align=left width="100%">

<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>'> </asp:Label>

</td>

</tr>

<tr>

<td valign="top" align=right width="100%">

<span style="font-weight: bold; color: blue">Customer Name :</span>

</td>

<td valign="top" align=left width="100%">

<asp:TextBox ID="EditCustomerNameTextBox" runat="server" Text='<%# Bind("Name") %>'> </asp:TextBox>

</td>

</tr>

<tr>

<td valign="top" align=right width="100%">

<span style="font-weight: bold; color: blue">Customer Surname:</span>

</td>

<td valign="top" align=left width="100%">

<asp:TextBox ID="EditCustomerSurnameTextBox" runat="server" Text='<%# Bind("Surname") %>'> </asp:TextBox>

</td>

</tr>

<tr>

<td valign="top" align=right width="100%">

<span style="font-weight: bold; color: blue">Customer Age:</span>

</td>

<td valign="top" align=left width="100%">

<asp:TextBox ID="EditCustomerAgeTextBox" runat="server" Text='<%# Bind("Age") %>'> </asp:TextBox>

</td>

</tr>

<tr>

<td valign="top" align=right width="100%">

<span style="font-weight: bold; color: blue">Customer Sexual:</span>

</td>

<td valign="top" align=left width="100%">

<asp:TextBox ID="EditCustomerSexualTextBox" runat="server" Text='<%# Bind("Sexual") %>'> </asp:TextBox>

</td>

</tr>

</table>

</td>

</tr>

<tr>

<td align="center"><hr />

<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"> </asp:LinkButton>

<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"> </asp:LinkButton>

</td>

</tr>

</table>

</EditItemTemplate>

<EmptyDataTemplate>

<table>

<tr>

<td align="center" ><hr />

<span style="font-weight: bold; font-size: x-large; color: blue"> No Customers Found ! </span>

<tr>

<td align="center"><hr />

<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="New" Text="New"> </asp:LinkButton>

</td>

</tr>

</table>

</EmptyDataTemplate>

<InsertItemTemplate>

<table width="100%">

<tr><td align="center"><hr />

<table width="100%">

<tr>

<td valign="top" align=right width="100%">

<span style="font-weight: bold; color: blue">Customer ID:</span>

</td>

<td valign="top" align=left width="100%">

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CustomerID") %>'> </asp:TextBox>

</td>

</tr>

<tr>

<td valign="top" align=right width="100%">

<span style="font-weight: bold; color: blue">Customer Name :</span>

</td>

<td valign="top" align=left width="100%">

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'> </asp:TextBox>

</td>

</tr>

<tr>

<td valign="top" align=right width="100%">

<span style="font-weight: bold; color: blue">Customer Surname:</span>

</td>

<td valign="top" align=left width="100%">

<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Surname") %>'> </asp:TextBox>

</td>

</tr>

<tr>

<td valign="top" align=right width="100%">

<span style="font-weight: bold; color: blue">Customer Age:</span>

</td>

<td valign="top" align=left width="100%">

<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Age") %>'> </asp:TextBox>

</td>

</tr>

<tr>

<td valign="top" align=right width="100%">

<span style="font-weight: bold; color: blue">Customer Sexual:</span>

</td>

<td valign="top" align=left width="100%">

<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Sexual") %>'> </asp:TextBox><br />

</td>

</tr>

<hr />

<tr>

<td align="center">

<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert"> </asp:LinkButton>

</td>

<td align="center">

<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"> </asp:LinkButton>

</td>

</tr>

</table>

</td></tr></table>

</InsertItemTemplate>

<HeaderTemplate>

<table><tr><td align="center">

<span style="font-weight: bold; font-size: x-large; color: blue">CUSTOMERS</span></td></tr>

<tr><td>

</HeaderTemplate>

<FooterTemplate>

</td></tr></table>

</FooterTemplate>

</asp:FormView>

</div>

</form>

</body>

</html>

Evet , son olarak artik formumuzun code behind tarafinda su sekilde hazirlayarak bu isi tamamlamis olacagiz.

Default.aspx Code Behind

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{}

protected void BtnCustomers_Click(object sender, EventArgs e)

{

FormView1.AllowPaging = true;

FormView1.DataSourceID = "ObjectDataSourceCustomers";

FormView1.DataKeyNames = new string[]{"CustomerID"};

CustomerList.Initialize();

FormView1.DataBind();

}

}

Programimizi çalistirdigimizda ilk gelen kisimda butona tiklayarak listeyi FormView kontrolüne baglayarak görüntüleyelim.

Yukarida da görüldügü gibi “Get Customer List” butonuna tiklaninca “Auto Format” a belirttigim gibi sekillenen FormView , kayitlari listelerken “Item Template” tarafinda belirttigimiz seklide listeleme isini gerçeklestirdi.

“Edit” linkine tikladigimizda görünüm “Edit Item Template” tarafinda belirtildigi gibi olacaktir :

Son olarak ekledigimiz kaydi da silem istersek “Delete “ linkine tiklamamiz yeterli olacaktir.

Sonuç olarak ; FromView kontrolü ve Collection mantigini incelemis olduk. Burada FormView kontrolünün DetailsView ile ayni oldugunu düsünebiliriz degisen tek sey yukarida da belirttigimiz gibi user-defined template olayinin FormView de daha kolay uygulanabilir olmasidir.

HASAN MANSUR

Kaynaklar :

http://www.developerfusion.co.uk/

http://www.asp.net/

MSDN

Yazar Hakkında Yorum Yok Yorum Yaz Yazara ait diğer Makaleler
Tema
Anket
ASP.NET MVC'yi Projeleriniz de kullandınız mı?


En Çok Okunan Makaleler
En Son Eklenen Makaleler
Soru Merkezinde Bekleyenler
Ana Sayfa | Hakkımızda | Editörler | Yazarlar | RSS RSS | İletişim
Yazılım Günlüğü 2007-2009 © Tüm Hakları Saklıdır.