失去了asp.net文本框对焦方法是什么? [英] Lost Focus method for asp.net textbox?

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

问题描述

如何写一个asp.net文本的方法失去重心的方法?请任何人有任何想法来写这个,跟我分享一下?

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.

让我们来看看code和一块去了它一块。

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

由于ASP.Net文本框不公开进行的onblur服务器端事件,你将不得不做手工。幸运的是,这是pretty容易实现。假设你有code这个小位在.aspx页面。你想,只要​​文本框失去焦点更新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有一个内置的被调用来触发回传,它有两个参数客户端功能


  1. 目标(控制导致事件的ID)

  2. 参数(可选信息,你想传递给服务器)

您的可能的只是wireup通过添加下面的属性和价值,你的文本框在标记的事件:

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

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

不过,框架有一个简单的方法来生成这个脚本为你的服务器端。在你的Page_Init方法,只需添加一个调用<一个href=\"http://msdn.microsoft.com/en-us/library/ms153112.aspx\"><$c$c>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);
}

使用标准服务器控件的事件,事件wireup和调用是自动通过实施<一个处理你href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackeventhandler.aspx\"><$c$c>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.

希望这有助于!

干杯,结果
乔希

Cheers,
Josh

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

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