最近在做一个文本框的自动提示,类似百度 ,google 查询的提示, 在文本框里输入内容后,通过AJAX 传到后台,查询出结果后返回到一个层里,点击这个层里的某条数据,这条数据就回填到文本框里,并触发文本框的 OnTextChanged 事件,该事件,会根据文本框里的值,查出一些数据,显示在页面上.
现在的问题是,每当我在层里选择好一条数据后,还没等它回添到文本框里时,已经触发了OnTextChanged 事件,造成查询结果是还没有回填时的数据.这个是什么问题啊.如何能让他在数据回添到文本框之后,才触发OnTextChanged 事件.
测试代码
<HTML>
<HEAD>
<script language="javascript" type="text/javascript" src="js/AjaxTextBox2.js"></script>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<style type="text/css" >
#txtsearchdiv div{COLOR: #000000; TEXT-DECORATION: none;
display:block;
width:60px;
height:24px;
line-height:24px;
background:#cccccc;
padding:1px 2px;
border:1px solid #999999;
cursor:pointer;
}
</style>
<script language="javascript">
function xx()
{
var txt = '<%=TextBox1.ClientID%>';
document.getElementById(txt).value = txt;
__doPostBack(txt,'');
}
function xxy(v)
{
var txt = '<%=TextBox1.ClientID%>';
document.getElementById('TextBox1').value = v;
__doPostBack('TextBox1','');
}
function gettext()
{
alert('2222');
var strings = "<div sytle='border:1px solid #cccccc;'onclick=xxy('1111');>dianwo jiuduile2 </div>";
strings+="<div sytle='border:1px solid #cccccc;'onclick=xxy('1111');>dianwo jiuduile2 </div>";
strings+="<div sytle='border:1px solid #cccccc;'onclick=xxy('1111');>dianwo jiuduile2 </div>";
strings+="<div sytle='border:1px solid #cccccc;'onclick=xxy('1111');>dianwo jiuduile2 </div>";
document.getElementById('txtsearchdiv').innerHTML=strings;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" runat="server" AutoPostBack="true" onkeyup="gettext();" autocomplete="off" OnTextChanged="txt_traffic_Id_TextChanged"></asp:TextBox>
<br /> <div id="txtsearchdiv" style="width:100px;background-color:White; display:block;border:1px solid #cccccc;" runat="server"></div>
<button onclick="xx();">XX</button>
<a href="#" onclick="xx();">xxxx</a>
</form>
</body>
</HTML>
------解决方案--------------------------------------------------------
http://www.cnblogs.com/zengxiangzhan/archive/2009/12/13/1623158