简体   繁体   English

asp.net文本框的丢失焦点方法?

[英]Lost Focus method for asp.net textbox?

How to write Lost focus method for asp.net text method? 如何为asp.net文本方法编写丢失焦点方法? 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. 让我们看一下代码,然后逐个讨论。

Since ASP.Net TextBox does not expose a server side event for OnBlur, you will have to do it manually. 由于ASP.Net TextBox不会为OnBlur公开服务器端事件,因此您必须手动执行此操作。 Fortunately this is pretty easy to achieve. 幸运的是,这很容易实现。 Suppose you have this small bit of code in your .aspx page. 假设您的.aspx页面中有这么少的代码。 You want to update a Label control server side whenever the TextBox loses focus. 每当TextBox失去焦点时,您都​​希望更新Label控制服务器端。

<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 has a built in client side function that gets called to trigger postbacks that takes two parameters: ASP.Net有一个内置的客户端函数 ,可以调用它来触发带有两个参数的回发:

  1. Target (the ID of the control causing the event) 目标(导致事件的控件的ID)
  2. Argument (optional information you would like to pass to the server) 参数(您希望传递给服务器的可选信息)

You could just wireup the event in markup by adding the following attribute and value to your TextBox: 可以通过将以下属性和值添加到TextBox中来标记事件的标记:

onblur="__doPostBack('tbOnBlur','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: 在Page_Init方法中,只需添加一个对GetPostBackEventReference的调用,并将其分配给“onblur”属性,如下所示:

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

With standard server control events, the event wireup and invocation is handled automagically for you by implementing IPostBackEventHandler . 使用标准服务器控件事件,可以通过实现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. 最后,如果您不介意深入挖掘框架,那么模拟服务器端事件并不是非常困难。

Hope this helps! 希望这可以帮助!

Cheers, 干杯,
Josh 玩笑

If you want the server to do something after the textbox loses focus you can add AutoPostback="True" and, if you don't want the postback to reload the whole page, use an UpdatePanel: 如果您希望服务器在文本框失去焦点后执行某些操作,则可以添加AutoPostback =“True”,如果您不希望回发重新加载整个页面,请使用UpdatePanel:

    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" 
                            OnTextChanged="TextBox1_TextChanged" />
        </ContentTemplate>
    </asp:UpdatePanel>

The function TextBox1_TextChanged can then do something with the text (serverside). 函数TextBox1_TextChanged然后可以对文本(serverside)执行某些操作。

if (!Page.IsPostBack)
    {
        txtName.Attributes.Add("onblur","alert('Hello world')");
    }

Why don't you use that. 你为什么不用它。 Lostfocus works same with: Lostfocus的工作原理与:

OnTextChanged="TextBox_TextChanged"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM