.net 通过输入文本框进行下拉式菜单的联想搜索(资料3)
2016-04-01 09:37
393 查看
对资料(2)做了一些改进,隐藏了下拉框,在使用的时候出现。
前台代码我直接复制了。数据库见资料(2)。
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default7.aspx.cs" Inherits="WebApplication1.Default7" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 40px">
<asp:TextBox ID="txtFilterCity" runat="server" onkeyup="filterCity(this);"></asp:TextBox>
<asp:DropDownList ID="ddlCityList" runat="server" AutoPostBack="True" style="display:none;"
onselectedindexchanged="ddlCityList_SelectedIndexChanged" Height="20px"
Width="149px">
</asp:DropDownList>
<br />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="拼音"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"
Width="255px"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="成语"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Width="255px"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="字词"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Height="90px" Width="256px"
TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Label ID="Label4" runat="server" Text="释义"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Height="138px" Width="256px"
TextMode="MultiLine"></asp:TextBox>
</div>
<script type="text/javascript">
var store = [];
window.onload = function () {
var citys = document.getElementById('ddlCityList');
for (var i = 0; i < citys.options.length; i++) {
var c = citys.options[i].text;
var city = {
id: citys.options[i].value,
text: citys.options[i].text
};
store.push(city);
}
}
function filterCity(o) {
var citys = document.getElementById('ddlCityList');
ddlCityList.style.display = "block";
citys.options.length = 0;
if (o.value != '') {
for (var j = 0; j < store.length; j++) {
if (store[j].text.indexOf(o.value) >= 0) {
var option = new Option();
option.value = store[j].id;
var a = store[j].text;
option.text = a.substring(a.length - 4, a.length);
citys.options.add(option);
}
}
} else {
for (var j = 0; j < store.length; j++) {
var option = new Option();
option.value = store[j].id;
var b = store[j].text;
option.text = b.substring(b.length - 4, b.length);
citys.options.add(option);
}
}
}
</script>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class Default7 : System.Web.UI.Page
{
SqlConnection myConnection = new SqlConnection("dat***B2s");
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
using (SqlConnection conn = new SqlConnection("data source=2***2s"))
{
SqlDataAdapter adapter1 = new SqlDataAdapter("select id, pysx, cy from t003", conn);
DataTable table1 = new DataTable();
adapter1.Fill(table1);
table1.Columns.Add("text", System.Type.GetType("System.String"), "pysx+'-'+cy");
ddlCityList.DataSource = table1.DefaultView;
ddlCityList.DataTextField = "text";
ddlCityList.DataValueField = "id";
ddlCityList.DataBind();
}
}
}
protected void ddlCityList_SelectedIndexChanged(object sender, EventArgs e)
{
string h;
string k;
h = this.ddlCityList.SelectedItem.ToString();
k = h.Substring(h.Length - 4, 4);
myConnection.Open();
string str2 = "
4000
select cy as cy0, py as py0, zcsy as zcsy0, sy as sy0, lj as lj0, xxsy as xxsy0, cc as cc0, en as en0 from t003 where cy = '" + k + "'";
SqlCommand com1 = new SqlCommand(str2, myConnection);
SqlDataReader SDX = com1.ExecuteReader();
while (SDX.Read())
{
TextBox2.Text = SDX["cy0"].ToString();
TextBox1.Text = SDX["py0"].ToString();
TextBox3.Text = SDX["zcsy0"].ToString();
TextBox4.Text = SDX["sy0"].ToString();
}
myConnection.Close();
txtFilterCity.Text = k;
}
protected void Open()
{
myConnection.Open();
}
protected void Close()
{
myConnection.Close();
}
}
}
![](https://img-blog.csdn.net/20160401093602326?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
前台代码我直接复制了。数据库见资料(2)。
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default7.aspx.cs" Inherits="WebApplication1.Default7" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 40px">
<asp:TextBox ID="txtFilterCity" runat="server" onkeyup="filterCity(this);"></asp:TextBox>
<asp:DropDownList ID="ddlCityList" runat="server" AutoPostBack="True" style="display:none;"
onselectedindexchanged="ddlCityList_SelectedIndexChanged" Height="20px"
Width="149px">
</asp:DropDownList>
<br />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="拼音"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"
Width="255px"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="成语"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Width="255px"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="字词"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Height="90px" Width="256px"
TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Label ID="Label4" runat="server" Text="释义"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Height="138px" Width="256px"
TextMode="MultiLine"></asp:TextBox>
</div>
<script type="text/javascript">
var store = [];
window.onload = function () {
var citys = document.getElementById('ddlCityList');
for (var i = 0; i < citys.options.length; i++) {
var c = citys.options[i].text;
var city = {
id: citys.options[i].value,
text: citys.options[i].text
};
store.push(city);
}
}
function filterCity(o) {
var citys = document.getElementById('ddlCityList');
ddlCityList.style.display = "block";
citys.options.length = 0;
if (o.value != '') {
for (var j = 0; j < store.length; j++) {
if (store[j].text.indexOf(o.value) >= 0) {
var option = new Option();
option.value = store[j].id;
var a = store[j].text;
option.text = a.substring(a.length - 4, a.length);
citys.options.add(option);
}
}
} else {
for (var j = 0; j < store.length; j++) {
var option = new Option();
option.value = store[j].id;
var b = store[j].text;
option.text = b.substring(b.length - 4, b.length);
citys.options.add(option);
}
}
}
</script>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class Default7 : System.Web.UI.Page
{
SqlConnection myConnection = new SqlConnection("dat***B2s");
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
using (SqlConnection conn = new SqlConnection("data source=2***2s"))
{
SqlDataAdapter adapter1 = new SqlDataAdapter("select id, pysx, cy from t003", conn);
DataTable table1 = new DataTable();
adapter1.Fill(table1);
table1.Columns.Add("text", System.Type.GetType("System.String"), "pysx+'-'+cy");
ddlCityList.DataSource = table1.DefaultView;
ddlCityList.DataTextField = "text";
ddlCityList.DataValueField = "id";
ddlCityList.DataBind();
}
}
}
protected void ddlCityList_SelectedIndexChanged(object sender, EventArgs e)
{
string h;
string k;
h = this.ddlCityList.SelectedItem.ToString();
k = h.Substring(h.Length - 4, 4);
myConnection.Open();
string str2 = "
4000
select cy as cy0, py as py0, zcsy as zcsy0, sy as sy0, lj as lj0, xxsy as xxsy0, cc as cc0, en as en0 from t003 where cy = '" + k + "'";
SqlCommand com1 = new SqlCommand(str2, myConnection);
SqlDataReader SDX = com1.ExecuteReader();
while (SDX.Read())
{
TextBox2.Text = SDX["cy0"].ToString();
TextBox1.Text = SDX["py0"].ToString();
TextBox3.Text = SDX["zcsy0"].ToString();
TextBox4.Text = SDX["sy0"].ToString();
}
myConnection.Close();
txtFilterCity.Text = k;
}
protected void Open()
{
myConnection.Open();
}
protected void Close()
{
myConnection.Close();
}
}
}
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- 数据库链接字符串查询网站
- 如何在 Linux/Windows/MacOS 上使用 .NET 进行开发
- DB2实例管理
- DB2实例管理
- 如何在 Linux 中安装微软的 .NET Core SDK
- 保障MySQL数据安全的14个最佳方法
- mysql问答汇集
- 第三章 数据库备份和还原
- 创建一个空的IBM DB2 ECO数据库的方法
- Access 2000 数据库 80 万记录通用快速分页类
- 开通一个数据库失败的原因的和解决办法
- 一个简单的asp数据库操作类
- CentOS下DB2数据库安装过程详解
- EasyASP v1.5发布(包含数据库操作类,原clsDbCtrl.asp)第1/2页
- C#.NET获取拨号连接的宽带连接方法
- sql2008 还原数据库解决方案
- Oracle 数据库自动存储管理-安装配置
- Oracle数据库执行脚本常用命令小结