看到在状态保持方面,尤其是需要在多页间进行状态保持的Stage,多了一种比较好的选择,特别是在创建Wizard Style WebApplication的时候
原文地址: http://www.codeproject.com/aspnet/ProcessContext.asp
1
using System;
2
using System.Web;
3
using System.Web.UI;
4
using System.Collections;
5
6
namespace Base4.Web.UI
7
{
8
/// <summary>
9
/// This class allows you to move data around (without using a database) between Postbacks and Redirects
10
/// that are part of the same process and working with the same data.
11
/// </summary>
12
public class ProcessContext
13
{
14
public static ProcessContext Current
15
{
16
get
17
{
18
//The overhead of creating a ProcessContext is minimal since it is just a proxy to the real data
19
//the reason we cache it in the HttpContext is that if we construct a new ProcessContext on each
20
//Call to ProcessContext.Current then we call RegisterHiddenField more than once per request
21
//which is dangerous
as we may end up storing our data against the wrong ID!
22
ProcessContext current = HttpContext.Current.Items["__PROCESSCONTEXT"] as ProcessContext;
23
if (current == null)
24
{
25
current = new ProcessContext();
26
HttpContext.Current.Items["__PROCESSCONTEXT"] = current;
27
}
28
return current;
29
}
30
}
31
public static string CurrentProcessID
32
{
33
get
34
{
35
return Current.ProcessID;
36
}
37
}
38
protected string _processID = null;
39
protected Hashtable _items = null;
40
41
/// <summary>
42
/// When the ProcessContext is constructed so postbacks function okay without a QUERYSTRING
43
/// redirect this Registers a Hidden Field to store the ProcessID in the form.
44
/// </summary>
45
protected ProcessContext()
46
{
47
(HttpContext.Current.Handler as Page).RegisterHiddenField("__PROCESSID",ProcessID);
48
}
49
/// <summary>
50
/// Accesses the Hashtable associated with this ProcessContext.
51
/// It creates a new Hashtable if not found. The Hashtable is stored in the users Session
52
/// keyed on the ProcessContext.ProcessID
53
/// </summary>
54
public Hashtable Items
55
{
56
get
57
{
58
if (_items == null)
59
{
60
_items = HttpContext.Current.Session[ProcessID] as Hashtable;
61
if (_items == null)
62
{
63
_items = new Hashtable();
64
HttpContext.Current.Session[ProcessID] = _items;
65
}
66
}
67
return _items;
68
}
69
}
70
/// <summary>
71
/// Figures out what the ProcessID of the current ProcessContext is
72
/// Checking in order -> QueryString, __PROCESSID field, or creating a new GUID
73
/// </summary>
74
public string ProcessID
75
{
76
get
77
{
78
if (_processID == null)
79
{
80
_processID = HttpContext.Current.Request.QueryString["ProcessID"] as string;
81
if (_processID == null)
82
{
83
_processID = HttpContext.Current.Request.Form["__PROCESSID"] as string;
84
if (_processID == null)
85
{
86
_processID = Guid.NewGuid().ToString();
87
}
88
}
89
}
90
return _processID;
91
}
92
}
93
/// <summary>
94
/// Convenience access to this.Items[key] using this[key]
95
/// </summary>
96
public object this[object key]
97
{
98
get
99
{
100
return Items[key];
101
}
102
set
103
{
104
Items[key] = value;
105
}
106
}
107
/// <summary>
108
/// Used to discard all data associated with this process
109
/// </summary>
110
public void Discard()
111
{
112
HttpContext.Current.Session.Remove(ProcessID);
113
}
114
/// <summary>
115
/// Used to empty the hashtable associated with this process
116
/// </summary>
117
public void Clear()
118
{
119
Items.Clear();
120
}
121
/// <summary>
122
/// Used to decorate a URL with the ProcessID so the ProcessContext
123
/// can be transfered to that URL
124
/// </summary>
125
/// <param name="url"></param>
126
public string AttachProcessToUrl(string url)
127
{
128
if (url.IndexOf("?") == -1)
129
url = url + "?";
130
else
131
url = url + "&";
132
url += string.Format("ProcessID={0}",ProcessID);
133
return url;
134
}
135
/// <summary>
136
/// Used to redirect to the URL provided and transfer the ProcessContext
137
/// to that URL too.
138
/// </summary>
139
/// <param name="url"></param>
140
/// <param name="endRequest"></param>
141
public void Redirect(string url, bool endRequest)
142
{
143
AttachProcessToUrl(url);
144
HttpContext.Current.Response.Redirect(url,endRequest);
145
}
146
}
147
}
148
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
特别保留:
Thanks Alex
Cheers!

浙公网安备 33010602011771号