Code Renaissance is about building great teams and great software. By exploring best practices, team interactions, design, testing and related skills Code Renaissance strives to help you create the team and codebase that you've always wanted.

Accessing Page data via Session in WebMethods

Asp.net public static methods that are marked with the attribute [webmethod] are available as Ajax calls in JavaScript. Unfortunately these methods can't access the Page object or any non-static values or objects.

The solution? Create one or more static properties and use them to store the data you need in your session.

static IAccount account
{

get
{
return (IAccount) HttpContext.Current.Session["MyPageAct"];
}

set
{
HttpContext.Current.Session["MyPageAct"] = value;
}

}
Now you can initialize this property in your page load event and because it's static it can also be accessed from your web method.

0 - What do you think?: