Ajax Server text box automatically fills in the value _ajax related

Source: Internet
Author: User
Tags connectionstrings
This increases the burden on the server. Behind himself he thought it over. Want to use Ajax to achieve such an effect. The code is as follows:
Foreground code:
Copy Code code 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 ">
<title> automatically populate user information with user name </title>
<script language= "javascript" type= "Text/javascript" src= "Ajax/jquery.js" ></script>
<script language= "javascript" type= "Text/javascript" >
Get the value of the User Name text box
function Writedatatext ()
{
var username=$ ("#Txtusename"). Val ();
if (username.length==0)
{
Alert ("The user name entered cannot be blank");
return;
}
Executing information about finding users by user name
$.ajax ({
Type: ' POST ',
URL: ' servertextboxdata.aspx ',
Data:{action: ' Action ', Username:username},
Success:writedatatextcallback
})
}
callback function to find information about a user by user name
function Writedatatextcallback (R)
{
if (r== "no")
{
Alert ("The user name entered does not exist.) Please re-enter ");
}
Else
{
var data=r;
var array=new array ();
Array=data.split (",");
Assign a value to a text box
$ ("#fullname"). Val (Array[0]);
$ ("#Email"). Val (array[1]);
$ ("#mobilphone"). Val (array[2]);
$ ("#qq"). Val (Array[3]);
}
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div align= "center" >
<table style= "text-align:center;width:800px" >
<tr style= "Background-color:yellow; width:800px ">
&LT;TD colspan= ' 2 ' style= "Text-align:center" > <font size= "5" color= "Red" > User Details </font></td>
</tr>
&LT;TR&GT;&LT;TD colspan= ' 2 ' style= "Text-align:left" > User name: <asp:textbox id= "Txtusename" runat= "Server" > </asp:TextBox></td></tr>
&LT;TR&GT;&LT;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>
&LT;TR&GT;&LT;TD style= "text-align:left;width:400px" > Mobile number: <asp:textbox runat= "Server" id= "Mobilphone" 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>

Background code:
Copy Code code 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"];
To add an event that loses focus to a server control the key to not refreshing the server control
TXTUSENAME.ATTRIBUTES.ADD ("onblur", "Writedatatext ()");
Txtusename.focus ();
if (straction = = "Action")
{
Get the name of the user input
String username = request["username"];
if (! Isusername (username))
{
Response.Clear ();
Response.ContentType = "Application/text";
Response.Write ("no");
Response.End ();
}
Else
{
InitData (username);
}
}
}
<summary>
Create Person: Zhou Xin
Date Created: 2009-06-11
Method Name: InitData
Role: Find out more about the user
</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>
Create Person: Zhou Xin
Date Created: 2009-06-11
Method Name: Isusername
Function: Returns a bool value to determine whether a 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.