asp.net 文本框的丢失焦点方法? [英] Lost Focus method for asp.net textbox?

查看:45
本文介绍了asp.net 文本框的丢失焦点方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为asp.net文本方法编写Lost focus方法?请问有人有什么想法写这个,分享给我吗?

How to write Lost focus method for asp.net text method? Please anybody have any idea to write this, share with me?

推荐答案

所以我知道每个人都展示了基本的客户端方法,这很好,但我想至少展示一个处理特定客户端事件的解决方案在服务器上.

So I know everyone has shown the basic client side approach, and that is fine, but I wanted to at least show a solution for handling a specific client side event on the server.

让我们看一下代码,然后一块一块地看一遍.

Lets take a look at the code, and go over it piece by piece.

由于 ASP.Net TextBox 不会为 OnBlur 公开服务器端事件,因此您必须手动进行.幸运的是,这很容易实现.假设您的 .aspx 页面中有这么一小段代码.您想在 TextBox 失去焦点时更新 Label 控件服务器端.

Since ASP.Net TextBox does not expose a server side event for OnBlur, you will have to do it manually. Fortunately this is pretty easy to achieve. Suppose you have this small bit of code in your .aspx page. You want to update a Label control server side whenever the TextBox loses focus.

<asp:Label ID="lblOnBlur" runat="server">On Blur Example</asp:Label><br />
<asp:TextBox ID="tbOnBlur" runat="server" ClientIDMode="Static" /><br />
<asp:Label ID="lblOutput" runat="server" />

ASP.Net 有一个内置的客户端函数,它被调用以触发采用两个参数的回发:

ASP.Net has a built in client side function that gets called to trigger postbacks that takes two parameters:

  1. 目标(引发事件的控件的 ID)
  2. 参数(您希望传递给服务器的可选信息)

可以通过将以下属性和值添加到您的文本框来将事件连接到标记中:

You could just wireup the event in markup by adding the following attribute and value to your TextBox:

onblur="__doPostBack('tbOnBlur','OnBlur');"

但是,该框架有一种简单的方法可以为您的服务器端生成此脚本.在您的 Page_Init 方法中,只需添加对 GetPostBackEventReference 并将其分配给onblur"属性以供您控制,如下所示:

However, the framework has an easy way to generate this script for you server side. In your Page_Init method, simply add a call to GetPostBackEventReference and assign it to the "onblur" attribute for you control like so:

protected void Page_Init(object sender, EventArgs e)
{
    var onBlurScript = Page.ClientScript.GetPostBackEventReference(tbOnBlur, "OnBlur");
    tbOnBlur.Attributes.Add("onblur", onBlurScript);
}

使用标准服务器控件事件,通过实现IPostBackEventHandler.这是一次性解决方案的大量工作,因此让我们通过检查请求参数来手动处理它.

With standard server control events, the event wireup and invocation is handled automagically for you by implementing IPostBackEventHandler. That is a lot of work for a one-off solution, so lets just handle it manually by inspecting the request params.

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

        HandleCustomPostbackEvent(ctrlName, args);
    }
}

private void HandleCustomPostbackEvent(string ctrlName, string args)
{
    //Since this will get called for every postback, we only
    // want to handle a specific combination of control
    // and argument.
    if (ctrlName == tbOnBlur.UniqueID && args == "OnBlur")
    {
        lblOutput.Text = "On Blur Event Handled Server Side!" + DateTime.Now;
    }
}

最后,如果您不介意深入研究一下框架,那么模拟服务器端事件并不是非常困难.

In the end it isn't terribly difficult to simulate server side events if you don't mind digging into the framework a little.

希望这有帮助!

干杯,
乔希

这篇关于asp.net 文本框的丢失焦点方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆