Tuesday, July 20, 2010

How to implement a session varable which is available since the first execution cycle - Asp.net

public partial class Test : Page
{
    private string item;
    public string Item
    {
        get
        {
            if (!string.IsNullOrEmpty(item)) return item;
            return (item = (string)(Session["item"] ?? string.Empty));
        }
        set
        {
            Session["Item"] = this.item = value;
        }
    }
 
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Item = DateTime.Now.ToString("hh:mm:ss");
    }
    protected override void OnLoad(EventArgs e)
    {
        this.Response.Write(this.Item);
    }
}

Other Types:
int? time;
public int Time
{
    get
    {
        if (this.time.HasValue) return time.Value;
        return (time = (int)(Session["time"] ?? 0)).Value;
    }
    set
    {
        Session["time"] = this.time = value;
    }
}
bool? isLive;
public bool IsLive
{
    get
    {
        if (this.isLive.HasValue) return isLive.Value;
        return (isLive = (bool)(Session["isLive"] ?? false)).Value;
    }
    set
    {
        Session["isLive"] = this.isLive = value;
    }
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...