您的位置:首页 > 编程语言 > ASP

ASP.NET2.0状态管理系列(5) 控件状态 Control state

2008-08-21 14:11 399 查看
A page developer can disable view state for the page or for an individual control for performance. However, control state cannot be disabled.

下面的例子解释控件状态和视图状态

1.创建自定义ASP.NET服务器控件

自定义控件代码

1 // IndexButton.cs

2 using System;

3 using System.ComponentModel;

4 using System.Security.Permissions;

5 using System.Web;

6 using System.Web.UI;

7 using System.Web.UI.WebControls;

8

9 namespace Samples.AspNet.CS.Controls

10 {

11 [

12 AspNetHostingPermission(SecurityAction.Demand,

13 Level = AspNetHostingPermissionLevel.Minimal),

14 AspNetHostingPermission(SecurityAction.InheritanceDemand,

15 Level = AspNetHostingPermissionLevel.Minimal),

16 ToolboxData("<{0}:IndexButton runat=\"server\"> </{0}:IndexButton>")

17 ]

18 public class IndexButton : Button

19 {

20 private int indexValue;

21

22 [

23 Bindable(true),

24 Category("Behavior"),

25 DefaultValue(0),

26 Description("The index stored in control state.")

27 ]

28 public int Index

29 {

30 get

31 {

32 return indexValue;

33 }

34 set

35 {

36 indexValue = value;

37 }

38 }

39

40 [

41 Bindable(true),

42 Category("Behavior"),

43 DefaultValue(0),

44 Description("The index stored in view state.")

45 ]

46 public int IndexInViewState

47 {

48 get

49 {

50 object obj = ViewState["IndexInViewState"];

51 return (obj == null) ? 0 : (int)obj;

52 }

53 set

54 {

55 ViewState["IndexInViewState"] = value;

56 }

57 }

58

59 protected override void OnInit(EventArgs e)

60 {

61 base.OnInit(e);

62 Page.RegisterRequiresControlState(this);

63 }

64

65 protected override object SaveControlState()

66 {

67 // Invoke the base class's method and

68 // get the contribution to control state

69 // from the base class.

70 // If the indexValue field is not zero

71 // and the base class's control state is not null,

72 // use Pair as a convenient data structure

73 // to efficiently save

74 // (and restore in LoadControlState)

75 // the two-part control state

76 // and restore it in LoadControlState.

77

78 object obj = base.SaveControlState();

79

80 if (indexValue != 0)

81 {

82 if (obj != null)

83 {

84 return new Pair(obj, indexValue);

85 }

86 else

87 {

88 return (indexValue);

89 }

90 }

91 else

92 {

93 return obj;

94 }

95 }

96

97 protected override void LoadControlState(object state)

98 {

99 if (state != null)

100 {

101 Pair p = state as Pair;

102 if (p != null)

103 {

104 base.LoadControlState(p.First);

105 indexValue = (int)p.Second;

106 }

107 else

108 {

109 if (state is int)

110 {

111 indexValue = (int)state;

112 }

113 else

114 {

115 base.LoadControlState(state);

116 }

117 }

118 }

119 }

120

121 }

122 }

2.编译控件为程序集

csc /t:library /out:Samples.AspNet.CS.Controls.dll D:\indexbutton.cs

3.测试自定义控件控件状态

在Web.config文件中注册自定义控件,每个页面都可以使用。

1 <pages>

2 <controls>

3 <add tagPrefix="aspSample"

4 namespace="Samples.AspNet.CS.Controls"

5 assembly="Samples.AspNet.CS.Controls" >

6 </add>

7 </controls>

8 </pages>

4.实现控件状态的条件

控件必须重写 OnInit,SaveControlState,LoadControlState方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: