转载:http://bbs.youkuaiyun.com/topics/350238970
此方法要用到 jQuery EasyUI 自己用的是jquery 1.4.2 和easyui 1.2.1
了解easyui:http://www.easyui.net/portal.php一:aspx页面,挺简单的
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<script src=
"../../../Scripts/jquery-1.4.2.min.js"
type=
"text/javascript"
></script>
<script src=
"../../../Scripts/jquery.easyui.min.js"
type=
"text/javascript"
></script>
<script type=
"text/javascript"
>
function GetName(para) {
$(
"#Text1"
).combobox({
url:
'VisaLogin.ashx?method=GetName¶='
+para,
valueField:
'Unit_Name'
,
textField:
'Unit_Name'
,
editable:
true
, width: 260
})
}
</script>
自动补全:<input id=
"Text1"
onkeyup=
"GetName(this.value)"
/>
|
二:ashx页面:VisaLogin.ashx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public
class
VisaLogin : IHttpHandler {
private
HttpContext context =
null
;
public
void
ProcessRequest(HttpContext context)
{
context.Response.ContentType =
"text/plain"
;
this
.context = context;
string
method = context.Request[
"method"
];
switch
(method)
{
case
"GetName"
:
GetName(context);
break
;
default
:
break
;
}
}
//自动补全
private
void
GetName(HttpContext context)
{
context.Response.ContentType =
"json"
;
string
para = context.Request[
"para"
];
String json = JsonUtils.ToJson(VisaComponent.GetAllUnit(para).Tables[0]);
context.Response.Write(json);
}
|
三:就这么两句代码了,另外要用到的两个方法:
JsonUtils.ToJson()(这个是用来把DataSet处理为easyui的接收的数据类型)
VisaComponent.GetAllUnit(para)(这个是从数据库获取数据的方法了)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
/// <summary>
/// Datatable转换为Json
/// </summary>
/// <param name="table">Datatable对象</param>
/// <returns>Json字符串</returns>
public
static
string
ToJson(DataTable dt)
{
StringBuilder jsonString =
new
StringBuilder();
jsonString.Append(
"["
);
DataRowCollection drc = dt.Rows;
for
(
int
i = 0; i < drc.Count; i++)
{
jsonString.Append(
"{"
);
for
(
int
j = 0; j < dt.Columns.Count; j++)
{
string
strKey = dt.Columns[j].ColumnName;
string
strValue = drc[i][j].ToString();
Type type = dt.Columns[j].DataType;
jsonString.Append(
"\""
+ strKey +
"\":"
);
strValue = StringFormat(strValue, type);
if
(j < dt.Columns.Count - 1)
{
jsonString.Append(strValue +
","
);
}
else
{
jsonString.Append(strValue);
}
}
jsonString.Append(
"},"
);
}
jsonString.Remove(jsonString.Length - 1, 1);
jsonString.Append(
"]"
);
return
jsonString.ToString();
}
|
另一个:
1
2
3
4
5
6
7
8
9
10
11
12
|
/// <summary>
/// 自动补全
/// </summary>
/// <returns></returns>
public
static
DataSet GetAllUnit(
string
unitName)
{
string
sql =
"SP_UNIT_GETALL"
;
SqlParameter[] para =
new
SqlParameter[] {
new
SqlParameter(
"@Unit_Name"
,unitName)
};
return
SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, sql,para);
}
|
四:这是数据库,和存储过程
1
2
3
4
5
6
7
8
9
10
|
create
table
T_UNIT(
Unit_Name
varchar
(50)
not
null
)
ALTER
PROCEDURE
[dbo].[SP_UNIT_GETALL]
@Unit_Name
varchar
(50)
AS
BEGIN
select
Unit_Name
from
T_UNIT
where
Unit_Name
like
@Unit_Name+
'%'
END
|