swollaws
漂泊中体会不到生活的味道,那是因为吃不到老妈烧的饭。

在使用ASP.NET的时候,经常需要使用客户端脚本。一般情况下我们都是直接在.aspx文件中编辑脚本或是包含.js文件。但是有些情况下我们需要动态的添加客户端脚本,为此ClientScriptManager类中提供了大量这样的方法。由于本人多数是使用非动态方式创建的,所以下面这些简要的总结若有不到之处,尽请指出。

1、RegisterStartupScript:该方法添加的脚本块在页面加载完成但页面的OnLoad事件引发之前执行,一般而言是一次性的脚本内容。由于这个方法注册的脚本时放在页面的底部,因此可以让我们很方便的使用页面中的元素。

2、RegisterClientScriptBlock:该方法在已呈现的页的顶部添加一个脚本块。由于这个方法放在页面的顶部,所以页面的中元素可能不能正常调用。该方法一般用于注册客户端事件。

3、RegisterClientScriptInclude:该方法主要用于注册一个脚本包含。除此之外它和RegisterClientScriptBlock的注意事项雷同。

4、RegisterOnSubmitStatement:该方法主要用于注册一个与OnSubmit事件绑定的脚本。

关于方法1-4的简单例子:

 

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    public void Page_Load(Object sender, EventArgs e)
    {
        String csname1 = "PopupScript";
        String csname2 = "ButtonClickScript";
        Type cstype = this.GetType();

        ClientScriptManager cs = Page.ClientScript;

        if (!cs.IsStartupScriptRegistered(cstype, csname1))
        {
            String cstext1 = "alert('Hello World');";
            cs.RegisterStartupScript(cstype, csname1, cstext1, true);
        }

        if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
        {
            StringBuilder cstext2 = new StringBuilder();
            cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
            cstext2.Append("Form1.Message.value='Text from client script.'} </");
            cstext2.Append("script>");
            cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
        }

        String csname = "OnSubmitScript";

        if (!cs.IsOnSubmitStatementRegistered(cstype, csname))
        {
            String cstext = "document.write('Text from OnSubmit statement');";
            cs.RegisterOnSubmitStatement(cstype, csname, cstext);
        }

        /*
            String csname3 = "ButtonClickScript";
            String csurl = "~/script_include.js";

            if (!cs.IsClientScriptIncludeRegistered(cstype, csname3))
            {
                cs.RegisterClientScriptInclude(cstype, csname3, ResolveClientUrl(csurl));
            }
        
            script_include.js 文件内容
            function DoClick() {Form1.Message.value='Text from include script.'}
         */

    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>ClientScriptManager Example</title>
    </head>
<body>
    <form id="Form1" runat="server">
        <input type="text" id="Message" />
        <input type="button" value="ClickMe" onclick="DoClick()" />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

 

5、GetPostBackEventReference和GetPostBackClientHyperlink:返回一个可以在客户端事件中使用的字符串,以便回发到服务器。总体上两者的用途是一样的,主要区别在于:前者返回的串以 "javascript:" 打头,而后者则没有。

 

<%@ Page Language="C#"  %>
<%@ Implements Interface="System.Web.UI.IPostBackEventHandler" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    public void RaisePostBackEvent(string eventArgument)
    {
        lbShow.Text = "Postback handled by " + this.ID.ToString() + ". <br/>" + "Postback caused by " + eventArgument.ToString() + ".";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScriptManager cs = Page.ClientScript;

        HtmlInputButton htmlButton = new HtmlInputButton();
        htmlButton.ID = "myButton";
        htmlButton.Value = "Click";
        htmlButton.Attributes.Add("onclick", cs.GetPostBackEventReference(this, htmlButton.ID.ToString()));//通过这种方式将客户端脚本与元素相应事件绑定起来

        phControls.Controls.Add(htmlButton);
        phControls.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));

        HtmlAnchor htmlA = new HtmlAnchor();
        htmlA.ID = "myAnchor";
        htmlA.InnerText = "Link";
        htmlA.HRef = cs.GetPostBackClientHyperlink(this, htmlA.ID.ToString());
        phControls.Controls.Add(htmlA);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ClientScriptManager Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label id="lbShow" runat="server" />
        <br />
        <asp:PlaceHolder id="phControls" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>

 

 

 

客户端文件内容:

<input name="myButton" type="button" id="myButton" value="Click" onclick="__doPostBack('__Page','myButton')" />&nbsp;&nbsp;<a href="javascript:__doPostBack('__Page','myAnchor')" id="myAnchor">Link</a>

 

还有其他部分方法有待补充...

 

posted on 2009-04-28 15:59  swollaw  阅读(249)  评论(0编辑  收藏  举报