This increases the burden on the server. He thought about it later. I want to use ajax to achieve this effect. The Code is as follows:
Front-end code:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "ServerTextBoxdata. aspx. cs" Inherits = "Default3" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> other user information is automatically filled with the user name </title>
<Script language = "javascript" type = "text/javascript" src = "ajax/jquery. js"> </script>
<Script language = "javascript" type = "text/javascript">
// Obtain the User Name text box Value
Function WritedataText ()
{
Var username = $ ("# Txtusename"). val ();
If (username. length = 0)
{
Alert ("the user name entered cannot be blank ");
Return;
}
// Execute the following command to find the user information:
$. Ajax ({
Type: 'post ',
Url: 'servertextboxdata. aspx ',
Data: {action: 'action', Username: username },
Success: WritedataTextcallback
})
}
// Callback function for searching user-related information by user name
Function WritedataTextcallback (r)
{
If (r = "no ")
{
Alert ("the user name entered does not exist. Enter ") again ");
}
Else
{
Var data = r;
Var array = new Array ();
Array = data. split (",");
// Assign a value to the text box
$ ("# Fullname"). val (array [0]);
$ ("# Email"). val (array [1]);
$ ("# Your Phone"). val (array [2]);
$ ("# Qq"). val (array [3]);
}
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div align = "center">
<Table style = "text-align: center; width: 800px">
<Tr style = "background-color: Yellow; width: 800px">
<Td colspan = '2' style = "text-align: center "> <font size =" 5 "color =" red "> User details </font> </td>
</Tr>
<Tr> <td colspan = '2' style = "text-align: left"> User name: <asp: textBox ID = "Txtusename" runat = "server"> </asp: TextBox> </td> </tr>
<Tr> <td style = "text-align: left; width: 400px"> User Full name: <asp: textBox ID = "fullname" runat = "server" ReadOnly = "true"> </asp: TextBox> </td> <td style = "text-align: left; width: 400px "> User Email: <asp: TextBox ID =" Email "ReadOnly =" true "runat =" server "> </asp: TextBox> </td> </tr>
<Tr> <td style = "text-align: left; width: 400px"> mobile phone number: <asp: textBox runat = "server" ID = "your phone" ReadOnly = "true"> </asp: TextBox> </td> <td style = "text-align: left; width: 400px ">
User QQ: <asp: TextBox runat = "server" ID = "qq" ReadOnly = "true"> </asp: TextBox> </td> </tr>
</Table>
</Div>
</Form>
</Body>
</Html>
Background code:
Copy codeThe Code is as follows:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
Public partial class Default3: System. Web. UI. Page
{
String StrAction = "";
Protected void Page_Load (object sender, EventArgs e)
{
StrAction = Request ["action"];
// The Key to adding events with no focus for the server control so that the server control is not refreshed
Txtusename. Attributes. Add ("onblur", "WritedataText ()");
Txtusename. Focus ();
If (StrAction = "action ")
{
// Obtain the name entered by the user
String username = Request ["Username"];
If (! Isusername (username ))
{
Response. Clear ();
Response. ContentType = "application/text ";
Response. Write ("no ");
Response. End ();
}
Else
{
InitData (username );
}
}
}
/// <Summary>
/// Created by Zhou Xin
/// Creation Time:
/// Method Name: InitData
/// Function: Search for user details
/// </Summary>
/// <Param name = "username"> </param>
Public void InitData (string username)
{
SqlConnection mycon = new SqlConnection ();
Mycon. ConnectionString = ConfigurationManager. ConnectionStrings ["BoBoConn"]. ToString ();
String SQL = "select Fullname, Email, MobilePhone, QQ from loginuser where username = '" + username + "'";
Mycon. Open ();
SqlCommand mycom = new SqlCommand (SQL, mycon );
SqlDataReader myda = mycom. ExecuteReader ();
While (myda. Read ())
{
String fullname = myda ["Fullname"]. ToString ();
String Email = myda ["Email"]. ToString ();
String MobilePhone = myda ["MobilePhone"]. ToString ();
String QQ = myda ["QQ"]. ToString ();
String array = fullname + "," + Email + "," + MobilePhone + "," + QQ;
Response. Clear ();
Response. ContentType = "application/text ";
Response. Write (array );
Response. End ();
}
}
/// <Summary>
/// Created by Zhou Xin
/// Creation Time:
/// Method Name: Isusername
/// Function: return the bool value to determine whether the user exists.
/// </Summary>
/// <Param name = "username"> </param>
/// <Returns> </returns>
Public bool Isusername (string username)
{
SqlConnection mycon = new SqlConnection ();
Mycon. ConnectionString = ConfigurationManager. ConnectionStrings ["BoBoConn"]. ToString ();
String SQL = "select count (*) from loginuser where username = '" + username + "'";
Mycon. Open ();
SqlCommand mycom = new SqlCommand (SQL, mycon );
Int n = (int) mycom. ExecuteScalar ();
Mycon. Close ();
If (n> 0)
{
Return true;
}
Else
{
Return false;
}
}
}
Effect: only the User Name text box is available before running
After the user enters the User name: After the mouse leaves the text box, the effect is as follows: