Is there a way to access the current session from within a substitution control?

832 views Asked by At

Looks like context parameter of asp:net substitution control method is missing the current session once the page is output cached.

Aside from getting the substitution control's content thru ajax or using some other method to store data that would otherwise be in session (like database) is there anything else I can do?

Is there a workaround to use native asp.net session in substitution control?

<asp:Substitution  ID="myid" MethodName="method1"></asp:Substitution>

public static string method1(HttpContext context) <--  [1]

[1] --- conext.session is null after page is cached

2

There are 2 answers

1
toxaq On

What happens if you ensure that context.Session isn't null before accessing the value? I've had session problems before, not when using the Substitution control though, because Context.Session isn't set for the first request a user makes. You may be having a related problem as it wouldn't sit in the standard page lifecycle.

According to ScottGu everything should be available.

This static method will be passed an HttpContext object at runtime that contains the standard ASP.NET Request, Response, User, Server, Session, Application intrinsics, and which you can then use ...

Tip/Trick: Implement "Donut Caching" with the ASP.NET 2.0 Output Cache Substitution Feature

0
teddy On

From the same blog.

re: Tip/Trick: Implement "Donut Caching" with the ASP.NET 2.0 Output Cache Substitution Feature

Friday, February 23, 2007 1:30 AM by ScottGu

Hi Scott,

I think the error you are seeing is because you are trying to access the session object.

Unfortunately I don't think you can access the session in this scenario - since the session object is never populate (since the page is never created).

Sorry!

Scott