对象失去焦点时自己动提交数据 V2

本文介绍了一种不依赖LinkButton的方法,在ASP.NET页面中,通过为TextBox控件注册OnBlur事件,实现当对象失去焦点时自动提交数据的功能。详细解释了如何在Page_Init事件中注册事件,以及在Page_Load事件中判断并处理IsPostBack来触发自定义的OnBlurHandle方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一年多前,Insus.NET有写过一篇 《对象失去焦点时自己动提交数据》,那一篇是依赖Linkbutton来做隐藏提交。是否有不用依赖Linkbutton方法呢? 答案是肯定的。

.aspx页只拉一个TextBox控件:

ExpandedBlockStart.gif View Code
 1  <% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>
 2 
 3  <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
 4 
 5  < html  xmlns ="http://www.w3.org/1999/xhtml" >
 6  < head  runat ="server" >
 7      < title ></ title >
 8  </ head >
 9  < body >
10      < form  id ="form1"  runat ="server" >
11      < asp:TextBox  ID ="TextBox1"  runat ="server" ></ asp:TextBox >
12      </ form >
13  </ body >
14  </ html >


.aspx.cs页中,首选在Page_Init事件,为TextBox注册OnBlur事件:

ExpandedBlockStart.gif View Code
  protected  void Page_Init( object sender, EventArgs e)
    {      
         this.TextBox1.Attributes.Add( " onblur ", Page.ClientScript.GetPostBackEventReference( this.TextBox1,  " OnBlur "));
    }


写一个onBlue事件,将替代LinkButton的Click事件:

ExpandedBlockStart.gif View Code
private  void OnBlurHandle( string ctrl,  string args)
    {       
         if (ctrl ==  this.TextBox1.UniqueID && args ==  " OnBlur ")
        {
             // 这里写提交到数据库中
        }
    }

 

然后在网页的Page_Load事件,判断是否IsPostBack。

ExpandedBlockStart.gif View Code
  protected  void Page_Load( object sender, EventArgs e)
    {
         if (IsPostBack)
        {
             var ctrl = Request.Params[Page.postEventSourceID];
             var args = Request.Params[Page.postEventArgumentID];

            OnBlurHandle(ctrl, args);
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值