Site Renkleri

.Net Dersleri

Kontrol Değerlerinin PostBack'de Cookie'lere yazılması



Sayfayı açtığımızda değerlerin yüklenmesi için daha önceden bu değerlerin cookie nesnelerine yazılması gerekmektedir.Bu işlem için OnPreRender metodunu override etmemiz gerekmektedir.Ardından sayfada belirtilen StoreControlItem değerlerini StoreControlItems listesinden okuyup, nesne özelliklerini ilgili Cookie'lere yazacağız.

protected override void OnPreRender(EventArgs e)
{
//StoreControlItems listesindeki StoreControlItem nesnelerine erişiliyor.
foreach (StoreControlItem sciItem in this.StoredControlItems)
{
Control ctlItem;
//Control türünden ctlItem nesnesine, StoreControlItem da belirtilen ID ye sahip kontrol atanıyor. 
if (mIsMasterPage)
ctlItem = this.Page.Master.FindControl(sciItem.ControlID);
else
ctlItem = this.FindControl(sciItem.ControlID);
//Eğer kontrol mevcutsa
if (ctlItem != null)
{
object PropertyValue = null;
try
{
//Her nesnenin özellik değerleri ilgili cookie lere kaydedilecektir..
//InvokeMember metodu ile kontrolün istenen özellik değeri PropertyValue değerine atanmaktadır.
PropertyValue = ctlItem.GetType().InvokeMember(sciItem.PropertyName,
BindingFlags.GetField | BindingFlags.GetProperty |
BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.IgnoreCase, null, ctlItem, null);
}
catch
{
throw new ApplicationException("Özellik okunamiyor " + sciItem.ControlID + " " + sciItem.PropertyName);
}

//Kontrolün ID sine ait cookie HttpCookie türünden myCook nesnesine atanıyor.
HttpCookie myCook = this.Page.Request.Cookies[sciItem.ControlID];
if (myCook == null)
{
//Eğer cookie client tarafında mevcut değilse, Yeni cookie nesnesi yaratılıyor.
myCook = new HttpCookie(sciItem.ControlID);
myCook.Expires = DateTime.Now.AddHours(1);
//Cookie'ye nesne özellik değeri yazılıyor.
//Ör: myCook["Checked"]="true";
myCook[sciItem.PropertyName] = PropertyValue.ToString();
try
{
this.Page.Response.Cookies.Remove(sciItem.ControlID);
}
catch
{
}
//Cookie client tarafında kaydediliyor.
this.Page.Response.Cookies.Add(myCook);
}
myCook.Expires = DateTime.Now.AddHours(1);
//Cookie'ye nesne özellik değeri yazılıyor.
//Ör: myCook["Checked"]="true";
myCook[sciItem.PropertyName] = PropertyValue.ToString();
try
{
this.Page.Response.Cookies.Remove(sciItem.ControlID);
}
catch
{
}
this.Page.Response.Cookies.Add(myCook);
}
}

base.OnPreRender(e);
}

Web Tasarımı | Site Tasarımı | Bilgisayar Dersleri | Hosting | Domain