Site Renkleri

.Net Dersleri

Kontrol Değerlerinin Sayfa Açılırken Atanması



Cookie lere yazılmış kontrol bilgilerinin sayfa yüklenirken okunması ve bu kontrollere atanması gerekmektedir.Bu işlem için OnInit metodunu override etmemiz gerekmektedir.Ardından sayfada belirtilen StoreControlItem değerlerini StoreControlItems listesinden okuyup, Cookie değerlerini Reflection ile özelliklerine erişeceğiz.

protected override void OnInit(EventArgs e)
{
if (!this.Page.IsPostBack)
{
//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)
{
try
{
object PropertyValue;
try
{
//Her nesnenin özellik değerleri nesne id'si ile kaydedilen cookie lerde olacaktır.
//Bu yüzden eğer nesnenin id'sini taşıyan cookie mevcut ise değeri okunuyor.
//Ör: this.Page.Request.Cookies["CheckBox1"]["Checked"];
PropertyValue = this.Page.Request.Cookies[sciItem.ControlID][sciItem.PropertyName];
}
catch
{
return;
}

//Kontrolümüzün üye bilgilerine ulaşıp cookie'den okuduğumuz değeri ilgili özelliğine atıyoruz.
Type ctlType = ctlItem.GetType();
MemberInfo[] miType = ctlType.GetMembers();
foreach (MemberInfo mi in miType)
{
if (mi.Name == sciItem.PropertyName)
{
PropertyInfo pi = (mi as PropertyInfo);

object CastPropertyValue = System.Convert.ChangeType(PropertyValue, pi.PropertyType);

ctlType.InvokeMember(mi.Name,
BindingFlags.SetField | BindingFlags.SetProperty |
BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.IgnoreCase,
null, ctlItem,
new object[] { CastPropertyValue });
}
}
}
catch
{
throw new ApplicationException("Özellik yazılamıyor " + sciItem.ControlID + " " + sciItem.PropertyName);
}
}
}
}

base.OnInit(e);
}

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