首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

动态添加脚本有关问题

2012-02-10 
动态添加脚本问题在 form 之后添加OK,在 /form 之前添加就不OK了,请问哪错了?//.aspxfile%@PageLangu

动态添加脚本问题
在 <form> 之后添加OK,在 </form> 之前添加就不OK了,请问哪错了?


//.aspx   file

<%@   Page   Language= "C# "   AutoEventWireup= "true "   CodeFile= "AddClientEvent.aspx.cs "   Inherits= "AddClientEvent "   %>

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

<html   xmlns= "http://www.w3.org/1999/xhtml "   >
<head   runat= "server ">
        <title> Add   Client   Event </title>
</head>
<body>
        <form   id= "form1 "   runat= "server ">
        <div>
                <input   id= "Submit1 "   type= "submit "   value= "Click   me "   onclick= "Click() "   />
                </div>
               
        </form>
</body>
</html>


//.aspx.cs   file

using   System;
using   System.Data;
using   System.Configuration;
using   System.Collections;
using   System.Web;
using   System.Web.Security;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.WebControls.WebParts;
using   System.Web.UI.HtmlControls;

public   partial   class   AddClientEvent   :   System.Web.UI.Page
{
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                //MSDN:Page.RegisterClientScriptBlock   方法已被否决。请改用   ClientScriptManager   类的   RegisterClientScriptBlock   方法。

                ClientScriptManager   csm   =   Page.ClientScript;

                string   strScript1   =   "script1 ";
                string   strScript2   =   "script2 ";
                //OK
                if   ((!Page.IsPostBack)   &&   (!csm.IsClientScriptBlockRegistered(strScript1)))
                {
                        string   ss1   =   "alert( '在 <form> 标记之后 ') ";
                        csm.RegisterClientScriptBlock(this.GetType(),   strScript1,   ss1,   true);
                }
                //Error
                if   ((!Page.IsPostBack)   &&   (!csm.IsStartupScriptRegistered(strScript2)))
                {
                        string   ss2   =   " <script   type=\ "text/javascript\ ">   function   Click(){   alert( '在标记之前 ');   } </script> ";


                        csm.RegisterStartupScript(this.GetType(),   strScript2,   ss2,   true);
                }

        }
}


[解决办法]
js函数最好放在 <head> </head> 标签内。

在 <body> </body> 标签内进行调用。

热点排行