开发者

LostFocus on text box event in asp

Hi I am currently developing a web app in ASP.net. I am creating a registration form where when the user enter a username they would like to use it performs a check on the database to see if the username already exists. I don't want this to be done in the text changed event because I don't want it to try and query the database each time a character is entered into the text box. When I want to do is when that text boxes loses focus, either by tapping to the next field or clicking on another field it then checks the database for the username. If the username already exits then a message like a tooltip is 开发者_开发百科placed to the side of the field.

How can I perform this action when the text box loses focus.


To call a server side method on a client side event you need to do the following:

1- Create the server side method:

void DoSomething(...) { ... }

2- Implement the System.Web.UI.IPostBackEventHandler.RaisePostBackEvent which take one string argument (You can assign the name to the value of this argument).:

public void RaisePostBackEvent(string eventArgument) 
{
        DoSomething(...);
}

3- Write a script to trigger post back:

function TriggerPostBack(control, arg){
    __doPostBack(control, arg);
}

4- Call the PostBack trigger function when needed:

<asp:TextBox .... onblur="TriggerPostBack('textBox', document.getElementById('txtUsername').value)" .. /> 


onblur is the event you are looking for. See this example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜