在Web控件中添加类似于执行 body 的 onload 事件行为的客户脚本的一种方式

在 Web 控件开发或 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

 1<?xml version="1.0" encoding="utf-8" ?>
 2<root>
 3    <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
 4        <xsd:element name="root" msdata:IsDataSet="true">
 5            <xsd:complexType>
 6                <xsd:choice maxOccurs="unbounded">
 7                    <xsd:element name="data">
 8                        <xsd:complexType>
 9                            <xsd:sequence>
10                                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
11                                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
12                            </xsd:sequence>
13                            <xsd:attribute name="name" type="xsd:string" />
14                            <xsd:attribute name="type" type="xsd:string" />
15                            <xsd:attribute name="mimetype" type="xsd:string" />
16                        </xsd:complexType>
17                    </xsd:element>
18                    <xsd:element name="resheader">
19                        <xsd:complexType>
20                            <xsd:sequence>
21                                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
22                            </xsd:sequence>
23                            <xsd:attribute name="name" type="xsd:string" use="required" />
24                        </xsd:complexType>
25                    </xsd:element>
26                </xsd:choice>
27            </xsd:complexType>
28        </xsd:element>
29    </xsd:schema>
30    <resheader name="ResMimeType">
31        <value>text/microsoft-resx</value>
32    </resheader>
33    <resheader name="Version">
34        <value>1.0.0.0</value>
35    </resheader>
36    <resheader name="Reader">
37        <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
38    </resheader>
39    <resheader name="Writer">
40        <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
41    </resheader>
42    <data name="Startup" type="System.String">
43        <value><![CDATA[
44<script language = "javascript"><!--
45if (typeof(pageClientScript) == "undefined")
46    alert("Unable to find script library. Make sure the url referenced in web.config is correct.");
47else
48    pageOnload();
49//-->
50</script>
51]]></value>
52    </data>
53    <data name="Client" type="System.String">
54        <value><![CDATA[
55<script language = "javascript"><!--
56var pageClientScript = "";
57
58function pageOnload() {
59    alert("Page onload!");
60}
61//-->
62</script>
63]]></value>
64    </data>
65</root>
66

posted on 2006-01-04 17:09  Easy Company  阅读(932)  评论(0编辑  收藏  举报

导航