您现在的位置: 设计龙 >> 网络编程 >> asp.net教程 >> 正文

ASP.NET添加客户端代码的几种方法

Author:x Link:www Hits: Update:2006-10-2 13:13:30
天极网摘 ViVi收藏夹 365Key 和讯网摘 POCO网摘

我们在ASP.NET的开发中,经常要使用客户端代码,如:javascript

当然可以在html中嵌入。但这里所说的是在服务器端动态向客户端添加客户端代码,

  1、运用page控件的RegisterClientScriptBlock方法:

             dim s as string=""

              s +="functon AlertMsg() {"

              s+="alert("Error");}"

               s+=""

               if (not is ClientScriptBlockRegistered("xxx")) then

                           RegisterClientScriptBlock("xxx",s)

                end if

在服务器端添加到Button 的click事件中:

               button.Attributes.Add("onclick","AlertMsg()")

这样,就可以将这段javascript代码,添加到客户端

 2、在运行时,直接通过增加控件的属性来增加客户端代码

如:

          button.Attributes.Add("onclick","alert('Error!');")

两种方法可以达到相同的效果。

   
  • 上一篇文章:
  • 下一篇文章:
  • [回到标题] [回到顶部]