Automatically fill in the Ajax server text box

Source: Internet
Author: User

This increases the burden on the server. He thought about it later. I want to use ajax to achieve this effect. Code As follows:
Front-end code: Copy code The 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 code The 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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.