asp.net如何保持视图状态?
展开全部
你说的视图状态是不是指ViewState,如果是,你只要保证你没有在你的项目文件中的任何位置设置过EnableViewState="false"就行了,任何位置包括:web.config,母板页,当前页头文件,cs文件,控件本身的属性等。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<%@ Page Language="VB" %>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <script runat="server">
4 ' Sample ArrayList for the page.
5 Dim PageArrayList As ArrayList
6
7 Function CreateArray() As ArrayList
8 ' Create a sample ArrayList.
9 Dim result As ArrayList
10 result = New ArrayList(4)
11 result.Add("item 1")
12 result.Add("item 2")
13 result.Add("item 3")
14 result.Add("item 4")
15 Return result
16 End Function
17
18 Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
19 If (Me.ViewState("arrayListInViewState") IsNot Nothing) Then
20 PageArrayList = CType(Me.ViewState("arrayListInViewState"), ArrayList)
21 Else
22 ' ArrayList isn't in view state, so it must be created and populated.
23 PageArrayList = CreateArray()
24 End If
25 ' Code that uses PageArrayList.
26 End Sub
27
28 Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
29 ' Save PageArrayList before the page is rendered.
30 Me.ViewState.Add("arrayListInViewState", PageArrayList)
31 End Sub
32 </script>
33
34 <html >
35 <head runat="server">
36 <title>View state sample</title>
37 </head>
38 <body>
39 <form id="form1" runat="server">
40 <div>
41 </div>
42 </form>
43 </body>
44 </html>
45
46 <%@ Page Language="C#" %>
47 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
48 <script runat="server">
49 // Sample ArrayList for the page.
50 ArrayList PageArrayList;
51
52 ArrayList CreateArray()
53 {
54 // Create a sample ArrayList.
55 ArrayList result = new ArrayList(4);
56 result.Add("item 1");
57 result.Add("item 2");
58 result.Add("item 3");
59 result.Add("item 4");
60 return result;
61 }
62
63 void Page_Load(object sender, EventArgs e)
64 {
65 if (ViewState["arrayListInViewState"] != null)
66 {
67 PageArrayList = (ArrayList)ViewState["arrayListInViewState"];
68 }
69 else
70 {
71 // ArrayList isn't in view state, so it must be created and populated.
72 PageArrayList = CreateArray();
73 }
74 // Code that uses PageArrayList.
75 }
76
77 void Page_PreRender(object sender, EventArgs e)
78 {
79 // Save PageArrayList before the page is rendered.
80 ViewState.Add("arrayListInViewState", PageArrayList);
81 }
82 </script>
83
84 <html >
85 <head runat="server">
86 <title>View state sample</title>
87 </head>
88 <body>
89 <form id="form1" runat="server">
90 <div>
91 </div>
92 </form>
93 </body>
94 </html
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <script runat="server">
4 ' Sample ArrayList for the page.
5 Dim PageArrayList As ArrayList
6
7 Function CreateArray() As ArrayList
8 ' Create a sample ArrayList.
9 Dim result As ArrayList
10 result = New ArrayList(4)
11 result.Add("item 1")
12 result.Add("item 2")
13 result.Add("item 3")
14 result.Add("item 4")
15 Return result
16 End Function
17
18 Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
19 If (Me.ViewState("arrayListInViewState") IsNot Nothing) Then
20 PageArrayList = CType(Me.ViewState("arrayListInViewState"), ArrayList)
21 Else
22 ' ArrayList isn't in view state, so it must be created and populated.
23 PageArrayList = CreateArray()
24 End If
25 ' Code that uses PageArrayList.
26 End Sub
27
28 Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
29 ' Save PageArrayList before the page is rendered.
30 Me.ViewState.Add("arrayListInViewState", PageArrayList)
31 End Sub
32 </script>
33
34 <html >
35 <head runat="server">
36 <title>View state sample</title>
37 </head>
38 <body>
39 <form id="form1" runat="server">
40 <div>
41 </div>
42 </form>
43 </body>
44 </html>
45
46 <%@ Page Language="C#" %>
47 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
48 <script runat="server">
49 // Sample ArrayList for the page.
50 ArrayList PageArrayList;
51
52 ArrayList CreateArray()
53 {
54 // Create a sample ArrayList.
55 ArrayList result = new ArrayList(4);
56 result.Add("item 1");
57 result.Add("item 2");
58 result.Add("item 3");
59 result.Add("item 4");
60 return result;
61 }
62
63 void Page_Load(object sender, EventArgs e)
64 {
65 if (ViewState["arrayListInViewState"] != null)
66 {
67 PageArrayList = (ArrayList)ViewState["arrayListInViewState"];
68 }
69 else
70 {
71 // ArrayList isn't in view state, so it must be created and populated.
72 PageArrayList = CreateArray();
73 }
74 // Code that uses PageArrayList.
75 }
76
77 void Page_PreRender(object sender, EventArgs e)
78 {
79 // Save PageArrayList before the page is rendered.
80 ViewState.Add("arrayListInViewState", PageArrayList);
81 }
82 </script>
83
84 <html >
85 <head runat="server">
86 <title>View state sample</title>
87 </head>
88 <body>
89 <form id="form1" runat="server">
90 <div>
91 </div>
92 </form>
93 </body>
94 </html
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询