<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function abc() {
var inputV = document.getElementById("in").value;
//根據(jù)瀏覽器判斷
if (/msie/i.test(navigator.userAgent)) //ie瀏覽器
{
document.getElementById("lbltext").innerText = inputV;
}
else {//非ie瀏覽器,比如Firefox
document.getElementById("lbltext").innerHTML = inputV; //火狐等瀏覽器的賦值方式
}
}
function InputT() {
var f = document.getElementById("inpContent");
var abc = document.getElementById("btnHelp");
document.getElementById("btnHelp").click(); //觸發(fā)Button的onclick事件
}
//為input 添加的keydown事件
function InputKeyDownFocus() {
//方向鍵的ASCII值:上:38,下:40
if (event.keyCode == "38" || event.keyCode == "40") {
document.getElementById("lst").focus(); //使ListBox獲得焦點
}
else {
document.getElementById("inpContent").focus();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
輸入內(nèi)容:
<br />
<input runat="server" id="inpContent" oninput="InputT()" onpropertychange="InputT()"
onkeyup="InputKeyDownFocus()" </br> />
<asp:ListBox runat="server" ID="lst" OnSelectedIndexChanged="lst_SelectedIndexChanged"
AutoPostBack="true"</asp:ListBox>
<asp:Button runat="server" ID="btnHelp" OnClick="btnHelp_Click" Text="隱藏按鈕" />
</div>
</form>
</body>
</html>
后臺cs代碼:
代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
enum Direction
{
Up, Right, Down, Left
}
Direction dir;
protected void Page_Load(object sender, EventArgs e)
{
btnHelp.Style.Add("display", "none");
}
protected void lstShow_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lItem = (ListBox)sender;
string lItemValue = lItem.SelectedItem.Text;
txtInput.Text = lItemValue;
}
/// summary
/// 前臺調(diào)用的方法
/// /summary
/// param name="sender"/param
/// param name="e"/param
protected void btnHelp_Click(object sender, EventArgs e)
{
string inputStr = inpContent.Value.Trim(); //文本框輸入系統(tǒng)
Listobject listNew = new Listobject();
listNew.Add("abc");
listNew.Add("abcde");
listNew.Add("bcd");
listNew.Add("bcdef");
listNew.Add("bcdagb");
listNew.Add("bbccaa");
listNew.Add("aabbdd");
listNew.Add("ccaabbdd");
lst.Items.Clear(); //清除原有值
int i = 1;
foreach (object obj in listNew)
{
//符合條件的數(shù)據(jù)
if (obj.ToString().Contains(inputStr))
{
lst.Style.Add("display", "block");
lst.Items.Add(new ListItem(obj.ToString(), "" + i));
i++;
}
}
if (lst.Items.Count 0)
{
lst.SelectedIndex = 0;
}
inpContent.Focus();
}
/// summary
/// ListBox下拉框的值改變時
/// /summary
/// param name="sender"/param
/// param name="e"/param
protected void lst_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lItem = (ListBox)sender;
lst.Style.Add("display", "block");
string lItemValue = lItem.SelectedItem.Text;
inpContent.Value = lItemValue;
lst.Focus();
}
哦了聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com