27

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

James123
  • 11,184
  • 66
  • 189
  • 343

4 Answers4

38

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. 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 has a built in client side function that gets called to trigger postbacks that takes two parameters:

  1. Target (the ID of the control causing the event)
  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:

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:

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. 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

Josh
  • 44,706
  • 7
  • 102
  • 124
17

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:

    <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).

Willem
  • 5,364
  • 2
  • 23
  • 44
  • Out of interest - how does the updatepanel stop the reload of the whole page – Kamal Oct 05 '10 at 14:51
  • @Kamal... it doesn't. An update panel does nothing to prevent the entire page from loading on the server. Essentially what happens is that a postback is performed client-side using XmlHttpRequest and then all but the contents of the UpdatePanel are thrown away and returned. It alleviates the "flicker" of a normal postback, but saves no time processing on the server. – Josh Oct 05 '10 at 15:34
  • 5
    It reloads the whole page but only sends the html of the updatepanel(s) and some javascript to update the contents in the response. The response can be much smaller than downloading the whole page and the browser doesn't have to re-render the page. This can speed up the process a little, the user will not notice any flickering and the experience is just much smoother. – Willem Oct 05 '10 at 18:38
  • when i press tab focus on textbox is lost, but still its not firing onchange event – C Sharper Mar 28 '14 at 05:34
  • Was already using update panels anyway just forgot about ontextchanged i like this answer. +1 – RustyH Apr 30 '14 at 21:43
  • Thanks, that works well. But I also have another control that inserts into my textbox as well as the user and this technique does not work for that. In that case, I just added an asp:Button, "Preview text" that is not "wired" to anything, which just causes a postback (I think). Together they seem to cover all the bases. – Zeek2 Feb 25 '20 at 13:51
4
if (!Page.IsPostBack)
    {
        txtName.Attributes.Add("onblur","alert('Hello world')");
    }
Zo Has
  • 12,599
  • 22
  • 87
  • 149
2

Why don't you use that. Lostfocus works same with:

OnTextChanged="TextBox_TextChanged"
elifekiz
  • 1,456
  • 13
  • 26