在Web控件中添加类似于执行 body 的 onload 事件行为的客户脚本的一种方式
文件名:DemoPageOnload.cs
1 using System;
2 using System.Web.UI;
3 using System.Web.UI.WebControls;
4 using System.ComponentModel;
5 using System.Resources;
6
7 namespace NobleSpirit.Demo.Web.UI.WebControls
8 {
9 /// <summary>
10 /// DemoPageOnload 的摘要说明。
11 /// </summary>
12 [DefaultProperty("Text"),
13 ToolboxData("<{0}:DemoPageOnload runat=server></{0}:DemoPageOnload>")]
14 public class DemoPageOnload : System.Web.UI.WebControls.WebControl
15 {
16 private string text;
17
18 [Bindable(true),
19 Category("Appearance"),
20 DefaultValue("")]
21 public string Text
22 {
23 get
24 {
25 return text;
26 }
27
28 set
29 {
30 text = value;
31 }
32 }
33
34 /// <summary>
35 /// 将此控件呈现给指定的输出参数。
36 /// </summary>
37 /// <param name="output"> 要写出到的 HTML 编写器 </param>
38 protected override void Render(HtmlTextWriter output)
39 {
40 output.Write(Text);
41 }
42
43 protected override void OnPreRender(EventArgs e)
44 {
45 base.OnPreRender (e);
46
47 if (Page.IsClientScriptBlockRegistered ("DemoPageOnload"))
48 return;
49
50 string script = null;
51 ResourceManager rm = new ResourceManager (typeof(NobleSpirit.Demo.Web.UI.WebControls.DemoPageOnload));
52
53 script = rm.GetString ("Client", System.Globalization.CultureInfo.CurrentCulture);
54 Page.RegisterClientScriptBlock ("DemoPageOnload", script);
55
56 script = rm.GetString ("Startup", System.Globalization.CultureInfo.CurrentCulture);
57 Page.RegisterStartupScript ("DemoPageOnload", script);
58 }
59
60 }
61 }
文件名:DemoPageOnload.resx
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
posted on 2006-01-04 17:09 Easy Company 阅读(935) 评论(0) 收藏 举报
浙公网安备 33010602011771号