先上代码:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %><!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"
EnableViewState="False"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label" EnableViewState="False"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TextBox1" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>using System;
using System.Data.SqlClient;public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=pifamysql;Persist Security Info=True;User ID=sa;Password=83723910");
SqlCommand cmd = new SqlCommand("select * from C_User where UserName='" + TextBox1.Text + "'",conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
Label1.Text = "已经注册";
else
Label1.Text = "可以注册";
conn.Close();
conn.Dispose();
}
}
之前没有用过ajax控件,今天想尝试一下,想用它完成检测用户名是否重复的功能,但是发现个很奇怪的问题。
程序运行之后,
1:我在文本框内输入未注册过的用户名,点Tab键(目的是为了让文本框失去焦点),然后提示可以注册。
2:我在文本框内输入已经注册过的用户名,点Tab键,然后没有任何提示,点回车,提示已经注册。
3:在步骤1的基础上,将文本框的内容改为已经注册过的用户名,点Tab键,提示无变化,仍提示可以注册,若点回车,提示已经注册。
4:在步骤2的基础上,将文本框的内容改为未注册的用户名,提示可以注册。让我感到郁闷的是,为什么只有当我在文本框中输入未注册过的用户名,在文本框失去焦点时,才会正常显示提示信息。
在文本框中输入已经注册过的用户名,必须点回车,才可以正常显示提示信息。郁闷啊!
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"
EnableViewState="False"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label" EnableViewState="False"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TextBox1" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>using System;
using System.Data.SqlClient;public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=pifamysql;Persist Security Info=True;User ID=sa;Password=83723910");
SqlCommand cmd = new SqlCommand("select * from C_User where UserName='" + TextBox1.Text + "'",conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
Label1.Text = "已经注册";
else
Label1.Text = "可以注册";
conn.Close();
conn.Dispose();
}
}
之前没有用过ajax控件,今天想尝试一下,想用它完成检测用户名是否重复的功能,但是发现个很奇怪的问题。
程序运行之后,
1:我在文本框内输入未注册过的用户名,点Tab键(目的是为了让文本框失去焦点),然后提示可以注册。
2:我在文本框内输入已经注册过的用户名,点Tab键,然后没有任何提示,点回车,提示已经注册。
3:在步骤1的基础上,将文本框的内容改为已经注册过的用户名,点Tab键,提示无变化,仍提示可以注册,若点回车,提示已经注册。
4:在步骤2的基础上,将文本框的内容改为未注册的用户名,提示可以注册。让我感到郁闷的是,为什么只有当我在文本框中输入未注册过的用户名,在文本框失去焦点时,才会正常显示提示信息。
在文本框中输入已经注册过的用户名,必须点回车,才可以正常显示提示信息。郁闷啊!
你同样可以在输入完毕后,鼠标随意点击文本框外的其他任何一处也是现实同样的效果。
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function CheckUserName()
{
var us=document.getElementById("txtname").value;
if(us!="")
{
createXMLHttpRequest();
var url= "RegistValidate.ashx?username="+escape(document.getElementById("txtname").value);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=ShowResult;
xmlHttp.send(null);
}
}
function ShowResult()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var s;
s=xmlHttp.responseText;
alert(s);
}
}
}
</script>
asp.net中一个大头就是服务器控件,但是由于各种原因,大家在实际开发中很少使用,但是很少使用不等于不会使用,咱得没事研究研究,看看人家控件怎么做的,咱好学习啊,了解为什么不好用,以后在开发的时候是不是也应该避免这个问题。
我只是想试着用用ajax控件,结果出现问题了。查了查百度,也没找到,郁闷啊!
建议用onblur()事件.
本来我也打算用ontext唱歌事件的,但是想一想,每输入一个字符就验证下,这对服务器造成多大的负担?
另外.
我参考了langsin的ajax教程.
用
onblur="jsfunction(parameter1,parameter2)"
这种方式把控件id和控件value传到js的function jsfunction(parameter1,parameter2)里面去.再在function中用get方式传值给另一个不含html代码的页面(我们只用了他的.cs类来写方法.然后从那里用Response.Write(result);Response.End())返回结果.接着在之前的function中用(xmlHttp.responseText获得返回的结果.进行if else...在其中更改页面控件值.部分js代码如下:
// jsfunction from <input type="text" id="a" onblur="jsfunction('a',valueOfthisText)/>"
function jsfunction(parameter1,parameter2)
{
createXMLHTTP();// create XMLHttpRequest object
var url="DisposeLoadEventFromRegister.aspx?parameter1="+parameter1;
xmlHttp.open("GET",url,true);//get is the way to send the request;url the the distination address;true means a nonsynchronous request;
xmlHttp.onreadystatechange= anotherJSFunctionName;
xmlHttp.send(null);
}function anotherJSFunctionName()
{
//document.body.innerHTML+='<div>readyState:'+xmlHttp.readyState+'</div>';//used to show the state value;
if(xmlHttp.readyState==4)// 4 means data has been received completely,the state is ready;0 means still not be initialized;1 means in initialization. 2 means begin to sending data. 3 means sending data.
{
if(xmlHttp.status==200)// 200 means finished and successed; 500 stands for inserting failed.
{
if(xmlHttp.responseText=="empty")
{
var obj= document.getElementById("imageIDOfText_a");
obj.src="Images/necessary.GIF";
obj.style.visibility="visible";
}
else
{
var obj= document.getElementById("imageIDOfText_a");
obj.src="Images/ok.GIF";
obj.style.visibility="visible";
}
}
}
}