从 ASP.NET 服务器控件插入客户端脚本

来源:互联网 发布:装修设计画图软件 编辑:程序博客网 时间:2024/06/02 14:55

http://www.microsoft.com/china/MSDN/library/archives/library/DNAspp/html/aspnet-injectclientsidesc.asp

(1)动态改变<asp:Button>外观
  <asp:Button id="Button1"onmouseout="this.style.backgroundColor='blue'"        onmouseover="this.style.backgroundColor='red'" runat="server" />

(2)脚本检验数据
   一、在页面中写上javascript的函数:
       <script language=javascript>
            function test(){
               alert("this is test!!");
            }
            function test2(){
               var state = confirm("是否删除?");
               return state;
               }

       </scirpt>
   二、在后置文件中的Page_Load()事件中邦定控件
       Button1.Attributes.Add("onclick","javascript:return test();");
  
   三、让<ASP:TextBox …… /> 获得焦点
        1.document.getElementById("myTextBox").focus();
        2.document.myForm1.myTextBox.focus();

   四、Page.RegisterStartupScript(key,script)
           允许ASP.NET服务器控件在System.Web.UI.Page中发出客户脚本块。
           key是运行时脚本的一个文本索引;script表示客户端脚本的正文文本。
           string str="<scr"+"ipt> alert('"+parReason.Value.ToString() + "');</scr"+"ipt>";
           this.RegisterStartupScript("reason",str);

原创粉丝点击