在aspx页面中添加客户端事件

首先感谢小寒( http://xh831213.cnblogs.com/)关于“ 一个在ASPX页面中弹出警告窗口的办法”的回复,他在留言中提供了另一种弹出窗口的办法
public   void  popmessage(Page p,String message) 

p.Response.Write(
"<body onload = javascript:alert('" + message + "');>"); 
}
 
使用函数:popmessage(
this , " 对不起,当前操作执行失败' " );
前几天有幸看了邵志东老师关于asp.net的事件的讲座深有感受,现将学到的技巧写出来与大家共赏,也算抛砖引玉。
本人写了一个例子关于如何在服务器端控件上添加客户端事件。首先创建一个页面
<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>

<! 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 > Untitled Page </ title >
   
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
        
< asp:Button ID = " Button1 "  runat = " server "  OnClick = " Button1_Click "  Text = " 客户端事件演示-1 "   />
        
< br  />
        
< asp:Button ID = " Button2 "  runat = " server "  OnClick = " Button2_Click "  Text = " 客户端事件演示-2 "   />< br  />
        
< asp:Button ID = " Button3 "  runat = " server "  Text = " 添加控件客户端事件 "  OnClick = " Button3_Click "   />< br  />

    
</ div >
    
</ form >
</ body >
</ html >

相应的cs代码
public  partial  class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!this.IsPostBack)
        
{
            
this.Button3.Attributes.Add("onclick""return confirm('确定吗?');");

        }

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
this.RegisterClientScriptBlock("ClientScript""<script language=javascript>alert('客户端事件演示-注册客户端事件!添加脚本在<form>后面');</script>");
    }

    
protected void Button2_Click(object sender, EventArgs e)
    
{
        
this.RegisterStartupScript("ClientScript""<script language=javascript>alert('客户端事件演示-注册客户端事件!添加脚本在</form>前面');</script>");
    }


    
}

这个例子中button1按钮添加一段javascript脚本在<form>后面,button2添加javascript脚本在</form>前面。
邵志东老师的asp.net事件的讲座下载地址asp.net事件的讲座

http://liughost.cnblogs.com/archive/2006/03/21/355341.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值