ajax 服务器文本框自动填值 这样的话就增加了服务器的负担。后面自己他细想了一下。想利用ajax去实现这样一个效果。代码如下: 前台代码: 复制代码 代码如下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ServerTextBoxdata.aspx.cs" Inherits="Default3" %> 通过用户名自动填充用户其他信息
用户详细信息
用户名称:
用户全名:用户邮箱:
手机号码: 用户QQ :
后台代码: 复制代码 代码如下: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"]; //为服务器控件添加失去焦点的事件 让服务器控件不刷新的关键 Txtusename.Attributes.Add("onblur", "WritedataText()"); Txtusename.Focus(); if (StrAction == "action") { //获取用户输入的名称 string username = Request["Username"]; if (!Isusername(username)) { Response.Clear(); Response.ContentType = "application/text"; Response.Write("no"); Response.End(); } else { InitData(username); } } } /// /// 创建人:周昕 /// 创建时间:2009-06-11 /// 方法名称:InitData /// 作用:查找用户的详细信息 /// /// 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(); } } /// /// 创建人:周昕 /// 创建时间:2009-06-11 /// 方法名称:Isusername /// 作用:返回bool值判断用户是否存在 /// /// /// 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; } } } 效果:运行前只有用户名文本框可用 当用户输入用户名称后:鼠标离开文本框后效果如下: