ASP.NET为textbox 添加 失去焦点事件 和回车失去焦点

    技术2022-05-20  42

    则当textbox 失去焦点的时候,触发控件1 的事件。

    控件1 的事件可以随意写。 当写为:textbox 的 change 事件时, 就在textbox 失去焦点的时候 执行 textbox 的change事件。 

    //*.aspx<%@ 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>无标题页</title></head><body>    <form id="form1" runat="server">    <div>        <asp:Label ID="lblMessage" runat="server"></asp:Label><br />        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="我被隐藏啦" style="display:none" />    </form></body></html>

    //*.csusing System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;

    public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            TextBox1.Attributes["onblur"] = ClientScript.GetPostBackEventReference(Button1, null);        }    }    protected void Button1_Click(object sender, EventArgs e)    {        lblMessage.Text = "文本框失去焦点";    }}

     

     

     

    页面中,再判断回车键

    document.οnkeypress=docKeyPress;

    //捕捉按键function docKeyPress(){    if (event.keyCode==13)    {        if (event.srcElement.type!='button')            event.keyCode=9;    }}

     

     

    //页面里回车到下一控件的焦点function Enter2Tab(e){        try        {                var ob = IsFireFox ? e.target : event.srcElement;                if(ob.tagName == "INPUT" && (ob.type == "text"   ||ob.type == "password" || ob.type == "checkbox" ||    ob.type == "radio" )   ||    ob.tagName == "SELECT")                {                        var key = IsFireFox ? e.which : event.keyCode;                        if (key == 13)                        {                                if (IsFireFox)                                {                                        event.which = 9;                                }                                else                                {                                        event.keyCode = 9;                                }                        }                }        }        catch(E){}

    }

     

     

    使用方法:  在textBox上可以使用 onKeyDown="docKeyPress();"  

     

    即可实现其事件监听.

     

    一来,后台去监听失焦点后的事件; 二来,监听敲回车后的监听事件.

     

    即可解决 诸如 系统登陆口上需要失去焦点自动判断套帐 等问题.

     


    最新回复(0)