其实要的功能很简单,两个textbox,在第一个textBox里输入一个值以后,根据这个值从数据库里搜索符合要求的数据,然后把找到的数据填写到第二个textbox里。
现在的麻烦就是用onchange以后,整个页面都刷新了,用户在做这个操作以前的设置都刷没了。。想用ajax写一下,但是不会。。麻烦各位给提示提示.应该怎么写...感激涕零...!~<td colspan="3" style="text-align: left; cursor:hand;">
<asp:TextBox ID="xao22" Text="" AutoCompleteType="LastName"
runat="server" ontextchanged="xao22_TextChanged" Width="108px" AutoPostBack="true"></asp:TextBox>
<input type="text" size="22" value="" readonly="readonly" id="xao221" name="xao221" runat="server" /><span class="rd">*</span>
<asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC3300"></asp:Label>
</td>protected void xao22_TextChanged(object sender, EventArgs e)
{
string strNum = this.txtBrandNum.Value.Trim();
BindGzdm(strNum); //绑定故障现象
string xao22_id = "";
string xao22_name = "";
xao22_id = this.xao22.Text.Trim();
cec.xao22 = xao22_id;
xao22_name = clb.GetWdValue2(cec);
if (xao22_name.Length == 0)
{
Response.Write("<script>alert('没有找到网点资料,请确认输入!')</script>");
this.xao22.Focus();
this.xao221.Value = "";
}
else
{
this.xao221.Value = xao22_name;
}
}
现在的麻烦就是用onchange以后,整个页面都刷新了,用户在做这个操作以前的设置都刷没了。。想用ajax写一下,但是不会。。麻烦各位给提示提示.应该怎么写...感激涕零...!~<td colspan="3" style="text-align: left; cursor:hand;">
<asp:TextBox ID="xao22" Text="" AutoCompleteType="LastName"
runat="server" ontextchanged="xao22_TextChanged" Width="108px" AutoPostBack="true"></asp:TextBox>
<input type="text" size="22" value="" readonly="readonly" id="xao221" name="xao221" runat="server" /><span class="rd">*</span>
<asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC3300"></asp:Label>
</td>protected void xao22_TextChanged(object sender, EventArgs e)
{
string strNum = this.txtBrandNum.Value.Trim();
BindGzdm(strNum); //绑定故障现象
string xao22_id = "";
string xao22_name = "";
xao22_id = this.xao22.Text.Trim();
cec.xao22 = xao22_id;
xao22_name = clb.GetWdValue2(cec);
if (xao22_name.Length == 0)
{
Response.Write("<script>alert('没有找到网点资料,请确认输入!')</script>");
this.xao22.Focus();
this.xao221.Value = "";
}
else
{
this.xao221.Value = xao22_name;
}
}
<asp:UpdatePanel runat="server" ID="panel" UpdateMode="Conditional">
<ContentTemplate>
<input type="text" size="22" value="" readonly="readonly" id="xao221" name="xao221" runat="server" /><span class="rd">*</span>
<asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC3300"></asp:Label> </ContentTemplate>
</asp:UpdatePanel>在第一二textbox的textchange 事件中加:
panel.update();
ajax异步查询<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>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function startRequest()
{
createXMLHttpRequest();
try
{
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "ajax.aspx?username="+document.getElementById("<%=xao22.ClientID %>").value, true);
xmlHttp.send(null);
}
catch(exception)
{
alert("xmlHttp Fail");
}
}
function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200 || xmlHttp.status == 0)
{
var param = xmlHttp.responseText;
document.getElementById("<%=xao221.ClientID %>").value=param;
}
}
}
</script>
<asp:TextBox ID="xao22" Text="" AutoCompleteType="LastName"
runat="server" onblur="startRequest();" Width="108px"></asp:TextBox>
<input type="text" size="22" value="" readonly="readonly" id="xao221" name="xao221" runat="server" /><span class="rd">*</span>
<asp:Label ID="Label1" runat="server" Text="Label" ForeColor="#CC3300"></asp:Label>
ajax.aspx.cs 和 你的提交页面同目录级别 protected void Page_Load(object sender, EventArgs e)
{
string userName = Request.QueryString["username"];
Response.Write(GetTxt2Str(userName));
Response.End();
}
private string GetTxt2Str(string uname)
{
//根据条件查询,操作数据库
return "str2";
}
$("#textbox1").focus(function(){
$.post("IHttpHandler.ashx",{"id",$("#textbox1").val()},function(result){
$("#textbox2").val(result);
});
});
});