JS 自动完成 AutoComplete(Ajax 查询)(2)


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;
//********************************************************
//创建日期: 2009-03-10
//作 者: Oloen
//內容说明: 自动完成后台查询页面
// Type:pssale 合同查询
// Type:psunit 房间查询
//********************************************************
/// <summary>
/// 自动完成后台查询页面
/// </summary>
public partial class Common_AutoComplete : System.Web.UI.Page
{
const string tbStyle = @"style=""color:#F7922E""";
/// <summary>
/// 过滤条件
/// </summary>
string Filter = string.Empty;
/// <summary>
/// 查询值
/// </summary>
string InputValue = string.Empty;
/// <summary>
/// 自动完成类别
/// Type:pssale 合同查询
/// Type:psunit 房间查询
/// </summary>
string Type = string.Empty;
/// <summary>
/// 返回结果字符
/// </summary>
string ReturnStr = string.Empty;
private void Page_Load(object sender, System.EventArgs e)
{
switch (Type.ToLower())
{
case "pssale":
case "psunit":
default:
AutoPSUnitNo();
break;
}
Response.Clear();
Response.ContentType = "text/xml";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.Write(ReturnStr);
Response.End();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Filter = Request.QueryString["Filter"] ?? "1=1";
InputValue = Request.QueryString["InputValue"] ?? "";
InputValue.Replace("'","''");
Type = Request.QueryString["Type"] ?? "";
}
/// <summary>
/// 售楼系统房间编号自动完成
/// </summary>
void AutoPSUnitNo()
{
if (!string.IsNullOrEmpty(InputValue))
{
ReturnStr = @"<table cellSpacing=""0"" cellPadding=""0""150px""center""0""Tmp_AutoComplete_tblResult""padding-left:5;padding-right:5; background-color:#FFFFFF;border:1px solid #999999;"">";
string Sql = string.Format(@"SELECT TOP 10 UnitID,UnitNo,ProjectNo,PhaseNo,BlockNo FROM View_PS_Unit WHERE UnitNo LIKE '%{0}%' AND {1}", InputValue, Filter);
if (Type.ToLower().Equals("pssale"))
Sql = string.Format(@"SELECT TOP 10 SaleID,UnitID,UnitNo,ContractNo,Name,SaleDate,SellingPrice,ProjectNo FROM View_PS_Sale_Unit WHERE UnitNo LIKE '%{0}%' AND {1}", InputValue, Filter);
using (SqlDataReader sdr = DataAccessHelper.ExecuteReader(Sql) as SqlDataReader)
{
if (sdr == null || !sdr.HasRows)
{
ReturnStr = string.Empty;
return;
}
while (sdr.Read())
{
string td = string.Format(@"<td15"" nowrap>{0}</td>", sdr["ProjectNo"].ToString());
//td += string.Format(@"<td15"" nowrap>{0}</td>", sdr["PhaseNo"].ToString());
//td += string.Format(@"<td15"" nowrap>{0}</td>", sdr["BlockNo"].ToString());
td += string.Format(@"<td15"" nowrapright"" {1}>{0}</td>", sdr["UnitNo"].ToString(), tbStyle);
if (Type.ToLower().Equals("pssale"))
{
ReturnStr += string.Format(@"<tr ReturnValue=""{0},{1},{2},{3},{4},{5},{6}"">{7}</tr>",
sdr["SaleID"].ToString(), sdr["UnitID"].ToString(),
sdr["UnitNo"].ToString(), sdr["ContractNo"].ToString(),
sdr["Name"].ToString(), sdr["SaleDate"].ToString(),
sdr["SellingPrice"].ToString(), td);
}
else
ReturnStr += string.Format(@"<tr ReturnValue=""{0},{1}"">{2}</tr>", sdr["UnitID"].ToString(), sdr["UnitNo"].ToString(), td);
}
}
ReturnStr += @"</table>";
}
}
}


3 调用部分

复制代码 代码如下:


<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb-2312" />
<script type="text/javascript" src="https://www.jb51.net/JS/AutoComplete.js"></script>
<title>无标题 1</title>
</head>

<body>
<input type="text">

<script>
var auto = new autoComplete('t1')
auto.Init(document.all.t1);

//选中后做的事情
function ReturnAutoComplete(ReturnValue)
{
alert(ReturnValue)
}
</script>
</body>

</html>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwzsy.html