Page.ClientScript.RegisterStartupScript用法 - 军军小站|张军博客

Page.ClientScript.RegisterStartupScript用法

系统 2155 0

使用类型、键、脚本文本和指示是否添加脚本标记的布尔值向 Page 对象注册启动脚本。

参数

type

要注册的启动脚本的类型。

key

要注册的启动脚本的键。

script

要注册的启动脚本文本。

addScriptTags

指示是否添加脚本标记的布尔值.

备注:

启动脚本由它的键和类型唯一标识。具有相同的键和类型的脚本被视为重复脚本。只有使用给定的类型和键对的脚本才能使用该页面进行注册。试图注册一个已经注册的脚本不会创建重复的脚本。

调用 IsStartupScriptRegistered 方法以确定具有给定的键和类型对的启动脚本是否已经注册,从而避免不必要的添加脚本尝试。

RegisterStartupScript 方法的此重载中,使用 addScriptTags 参数可指示 script 参数中提供的脚本是否包装在 <script> 元素块中。将 addScriptTags 设置为 true 指示脚本标记将自动添加。

RegisterStartupScript 方法添加的脚本块在页面加载完成但页面的 OnLoad 事件引发之前执行。

 

        示例
      
           

 <%@ Page Language=
          
            "
          
          
            C#
          
          
            "
          
          %>

<script runat=
          
            "
          
          
            server
          
          
            "
          
          >


          
            public
          
          
            void
          
          
             Page_Load(Object sender, EventArgs e)

{


          
          
            //
          
          
             Define the name and type of the client scripts on the page.
          
          

String csname1 = 
          
            "
          
          
            PopupScript
          
          
            "
          
          
            ;

String csname2 
          
          = 
          
            "
          
          
            ButtonClickScript
          
          
            "
          
          
            ;

Type cstype 
          
          = 
          
            this
          
          
            .GetType();


          
          
            //
          
          
             Get a ClientScriptManager reference from the Page class.
          
          

ClientScriptManager cs =
          
             Page.ClientScript;


          
          
            //
          
          
             Check to see if the startup script is already registered.
          
          
            if
          
           (!
          
            cs.IsStartupScriptRegistered(cstype, csname1))

{

String cstext1 
          
          = 
          
            "
          
          
            alert('Hello World');
          
          
            "
          
          
            ;

cs.RegisterStartupScript(cstype, csname1, cstext1, 
          
          
            true
          
          
            );

}


          
          
            //
          
          
             Check to see if the client script is already registered.
          
          
            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
          
          
            );

}

}


          
          </script>

<html>

<head>

<title>ClientScriptManager Example</title>

</head>

<body>

<form id=
          
            "
          
          
            Form1
          
          
            "
          
          
            

runat
          
          =
          
            "
          
          
            server
          
          
            "
          
          >

<input type=
          
            "
          
          
            text
          
          
            "
          
           id=
          
            "
          
          
            Message
          
          
            "
          
          > <input type=
          
            "
          
          
            button
          
          
            "
          
           value=
          
            "
          
          
            ClickMe
          
          
            "
          
           onclick=
          
            "
          
          
            DoClick()
          
          
            "
          
          >

</form>

</body>

</html>



  


          
            //
          
          
            ASP.NET后台页面跳转  
          
          

Page.ClientScript.RegisterStartupScript(Page.GetType(), 
          
            ""
          
          , 
          
            "
          
          
            <script>if(confirm('保存成功!是否继续添加?')){location.href='ProductonAdd.aspx'}else{location.href='ProductonList.aspx'}</script>
          
          
            "
          
          
            );




          
          
            //
          
          
            后台弹出确定框
          
          
            

ClientScript.RegisterStartupScript(GetType(), 
          
          
            "
          
          
            message
          
          
            "
          
          , 
          
            "
          
          
            <script>alert('请正确输入!');</script>
          
          
            "
          
          
            );




          
          
            //
          
          
            ASP.NET后台页面跳转
          
          
            

Page.ClientScript.RegisterStartupScript(Page.GetType(), 
          
          
            ""
          
          , 
          
            "
          
          
            <script>alert('数据添加成功!');{location.href='ProductonList.aspx'}</script>
          
          
            "
          
          
            );



或



Page.ClientScript.RegisterStartupScript(
          
          
            typeof
          
          (
          
            string
          
          ), 
          
            ""
          
          , 
          
            "
          
          
            <script>window.location.href='AdminMain.aspx';</script>
          
          
            "
          
          
            );




          
          
            //
          
          
            后台弹出文本框
          
          

ScriptManager.RegisterStartupScript(Page, 
          
            typeof
          
          (
          
            string
          
          ), 
          
            "
          
          
            popUp
          
          
            "
          
          , 
          
            "
          
          
            window.open('rptView.aspx','打印预览','toolbar=no,location=no,scrollbars=yes,top=200px,left=200px,width=904px,height=650px')
          
          
            "
          
          , 
          
            true
          
          
            );





 小技巧:



 

后台:





  
          
          
            protected
          
          
            void
          
           Button1_Click(
          
            object
          
          
             sender, EventArgs e)

        {

            
          
          
            string
          
           temp = Request[
          
            "
          
          
            he
          
          
            "
          
          ].ToString();
          
            //
          
          
            通过request[控件name(非id)]来引用前台的input控件
          
          

            Page.ClientScript.RegisterStartupScript(
          
            this
          
          .GetType(), 
          
            "
          
          
            js
          
          
            "
          
          , 
          
            "
          
          
            alert('hello
          
          
            "
          
           + temp + 
          
            "
          
          
            ')
          
          
            "
          
          , 
          
            true
          
          );
          
            //
          
          
            alert添加参数 
          
          
                    }





前台:






          
          <html xmlns=
          
            "
          
          
            http://www.w3.org/1999/xhtml
          
          
            "
          
          >

<head runat=
          
            "
          
          
            server
          
          
            "
          
          >

    <title></title>

</head>

<body>

    <form id=
          
            "
          
          
            form1
          
          
            "
          
           runat=
          
            "
          
          
            server
          
          
            "
          
          >

   

    <fieldset>

    <legend>
          
            

    健康信息

    
          
          </legend>
          
            

    高度:
          
          <input name=
          
            "
          
          
            he
          
          
            "
          
           type=
          
            "
          
          
            text
          
          
            "
          
          />
          
            

    体重: 
          
          <input type=
          
            "
          
          
            text
          
          
            "
          
           />

    </fieldset>

    <asp:Button ID=
          
            "
          
          
            Button1
          
          
            "
          
           runat=
          
            "
          
          
            server
          
          
            "
          
           Text=
          
            "
          
          
            Button
          
          
            "
          
           onclick=
          
            "
          
          
            Button1_Click
          
          
            "
          
           />

       </form>

</body>

</html>
        

 

         
      

 

Page.ClientScript.RegisterStartupScript用法小结


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论