This article will introduce you
ASP. NET implements no refreshing detection of user names The function is: when the user enters the user name, the user name text box loses focus, it checks whether the user name is repeated, you do not need to click a button to check the status. (many websites now have this function ).
The following describes how to implement this function.Code:
Front-end code:
| The following is a reference clip:
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
VaR XMLHTTP = NULL;
Function checkuser ()
{
Try
{
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
}
Catch (err1)
{
Try
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Catch (err2)
{
XMLHTTP = new XMLHttpRequest ();
}
}
VaR text1 = Document. getelementbyid ("text1 ");
VaR url = "chkuser. aspx? User = "+ text1.value;
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = onhandler;
XMLHTTP. Send (null );
}
Function onhandler ()
{
If (XMLHTTP. readystate = 4)
{
VaR isvalid = XMLHTTP. responsetext;
VaR exists = Document. getelementbyid ("exsits ");
Exists. innerhtml = isvalid. substring (0, 4 );
}
}
</SCRIPT>
<Input id = "text1" type = "text" onblur = "checkuser ()"/> <Label D = "exsits"> </label> <br/> |
Create a chkuser. aspx and change the background code:
| The following is a reference clip:
Protected void page_load (Object sender, eventargs E)
{
String user = This. Request. querystring ["user"];
If (userexist (User ))
{
Response. Write ("exist ");
}
Else
{
Response. Write ("nonexistent ");
}
}
private bool userexist (string username)
{< br> classlib. employee EMP = new classlib. employee ();
return EMP. empnameexist (username);
}
|