AJAX 无刷新 文本框提示条,用层来提示[原创]

本文介绍如何使用ASP.NET结合AJAX实现文本框输入时自动填充下拉框的功能。通过JavaScript处理键盘事件,根据输入内容从服务器获取匹配项,并以下拉列表形式展示供用户选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据"AJAX基本应用之:根据输入内容自动填充下拉框。(ASP.NET版)[转] ",更改的提示,可用方向盘上下选择需要的内容,文本框无记录时,按删除键自动进行提示

 

前台代码

<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="ajax10.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>WebForm2</title>
        
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        
<meta content="C#" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
        
<script language="javascript">
        var index,which;
        function Text1_onkeyup(str)
        
{
        
        var s
=document.getElementById(str).getBoundingClientRect();//得控件绝对位置   ;getElementById(str)查找对象
        document.getElementById("div1").style.left=s.left;
        document.getElementById(
"div1").style.top=s.bottom;
        document.getElementById(
"div1").style.width=document.getElementById(str).style.width;
        
        
switch (window.event.keyCode)
        
{
        
            
case 13//回车
                document.getElementById("div1").style.visibility="hidden";
                
break;
            
case 40://向右箭头
            case 39//向下箭头
                if (index < document.getElementById("div1").childNodes.length) 
                
{
                    index
=index+1;
                }

                
for (i=1;i<=document.getElementById("div1").childNodes.length;i++)
                
{
                    
if (i==index)
                    
{
                        document.getElementById(str).value
=document.getElementById("div1").childNodes[index-1].innerText;//取值  ;   innerHTML:取代码
                        document.getElementById("div1").childNodes[index-1].style.backgroundColor="lightblue";
                    }

                    
else
                    
{
                        document.getElementById(
"div1").childNodes[i-1].style.backgroundColor="white";//背景变白
                        
                    }

                }

                
break;
            
case 38://向左箭头
            case 37://向上箭头 
                if (index>1
                
{
                    index
=index-1;
                }

                
for (i=1;i<=document.getElementById("div1").childNodes.length;i++)
                
{
                    
if (i==index)
                    
{
                        document.getElementById(str).value
=document.getElementById("div1").childNodes[index-1].innerText;//.substr(1);
                        document.getElementById("div1").childNodes[index-1].style.backgroundColor="lightblue";
                    }

                    
else
                    
{
                        document.getElementById(
"div1").childNodes[i-1].style.backgroundColor="white";
                    }

                }

                
break;
            
default:
                index
=0;
                which
=str;
                
if(str=="TextBox1")
                
{
                    stuInfoView.getMatchedStuId(document.getElementById(str).value,getGG_callBack);
                }

                
if(str=="TextBox2")
                
{
                    stuInfoView.getB_KH_name(document.getElementById(str).value,getGG_callBack);
                }

                
if(str=="TextBox3")
                
{
                    stuInfoView.getC_CP2_name(document.getElementById(str).value,getGG_callBack);
                }

        }

    }

    String.prototype.Trim 
= function() 
        

            
return this.replace(/(^s*)|(s*$)/g, ""); 
        }
 
    function getGG_callBack(response)
    
{
        
if(response.value.length != 0)
        
{                      
      var ds 
= response.value;
                     
           var s;
           s
="";
            
for(var i=0;i<response.value.length;++i)
        
{
       var sd;
             
if(which=="TextBox1")
             
{
                s
=+ "<div style="" class="gotTitle" onmouseover="javascript:this.style.backgroundColor='lightblue'" onclick="return gotTitle_onclick()">" + response.value[i].Trim() + "</div>";
             }

             
if(which=="TextBox2")
             
{
                s
=+ "<div style="" class="gotTitle" onmouseover="javascript:this.style.backgroundColor='lightblue'" onmouseout="javascript:this.style.backgroundColor='white'" onclick="return gotTitle_onclick()">" + response.value[i].Trim() + "</div>";
             }

              
if(which=="TextBox3")
             
{
                s
=+ "<div style="" class="gotTitle" onmouseover="javascript:this.style.backgroundColor='lightblue'" onclick="return gotTitle_onclick()">" + response.value[i].Trim() + "</div>";
             }

        }
 
        document.getElementById(
"div1").innerHTML=s;
            document.getElementById(
"div1").style.visibility="visible";               
           
         }

      
return
     }

    function gotTitle_onclick()
    
{
        
if (window.event.srcElement.className="gotTitle")
        
{
            document.getElementById(str).value
=window.event.srcElement.innerText;//.substr(1);
            document.getElementById("div1").style.visibility="hidden";
        }

    }


    function gotTitle_onblur()
    
{
        document.getElementById(
"div1").style.visibility="hidden";
    }

        
</script>
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<FONT face="宋体">
                
<div class="list" id="div1" onblur="return gotTitle_onblur()" style="Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 8px; BACKGROUND-COLOR: #7d84b9"></div>
                
<asp:textbox id="TextBox1" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 216px"
                    runat
="server"></asp:textbox><asp:textbox id="TextBox2" style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 176px"
                    runat
="server"></asp:textbox>
                
<asp:TextBox id="TextBox3" style="Z-INDEX: 104; LEFT: 112px; POSITION: absolute; TOP: 136px"
                    runat
="server"></asp:TextBox>
                
<asp:TextBox id="TextBox4" style="Z-INDEX: 105; LEFT: 112px; POSITION: absolute; TOP: 96px" runat="server"></asp:TextBox>
                
<asp:TextBox id="TextBox5" style="Z-INDEX: 106; LEFT: 112px; POSITION: absolute; TOP: 56px" runat="server"></asp:TextBox></FONT></form>
    
</body>
</HTML>

 

后台代码

 

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ajax10
{
    
/// <summary>
    
/// WebForm2 的摘要说明。
    
/// </summary>

    public class WebForm2 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.TextBox TextBox2;
        
protected System.Web.UI.WebControls.TextBox TextBox3;
        
protected System.Web.UI.WebControls.TextBox TextBox4;
        
protected System.Web.UI.WebControls.TextBox TextBox5;
        
protected System.Web.UI.WebControls.TextBox TextBox1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            Ajax.Utility.RegisterTypeForAjax(typeof(stuInfoView));
            
this.TextBox1.Attributes.Add("onkeyup","Text1_onkeyup('TextBox1');");
            
this.TextBox1.Attributes.Add("onblur","gotTitle_onblur();");
            
this.TextBox2.Attributes.Add("onkeyup","Text1_onkeyup('TextBox2');");
            
this.TextBox2.Attributes.Add("onblur","gotTitle_onblur();");
            
this.TextBox3.Attributes.Add("onkeyup","Text1_onkeyup('TextBox3');");
            
this.TextBox3.Attributes.Add("onblur","gotTitle_onblur();");
        }


        
Web 窗体设计器生成的代码
    }

}

AJAX调用类

 

using System;
using System.Text;
using System.Data;

namespace ajax10
{
    
/// <summary>
    
/// stuInfoView 的摘要说明。
    
/// </summary>

    public class stuInfoView
    
{
        
public stuInfoView()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
/// <summary>
        
/// 读取AJAX表中数据
        
/// </summary>
        
/// <param name="stuId"></param>
        
/// <returns></returns>

        [Ajax.AjaxMethod()]
        
public static string[] getMatchedStuId(string stuId)
        
{
            DataSet ds 
= INC.DB.Query("select name from ajax ");
            DataRow[] drs 
= ds.Tables[0].Select("name like '"+stuId+"%'");
            
if(drs != null)
            
{
                
string[] result = new string[drs.Length];
                
for(int i=0;i<drs.Length;i++)
                    result[i] 
= Convert.ToString(drs[i]["name"]);
                
return result;
            }

            
return null;
        }

        
/// <summary>
        
/// 读取客户简称
        
/// </summary>
        
/// <param name="kh_jc"></param>
        
/// <returns></returns>

        [Ajax.AjaxMethod()]
        
public static string[] getB_KH_name(string kh_jc)
        
{            
            DataSet ds 
= INC.DB.Query("select kh_jc from b_kh where kh_jc like '"+kh_jc+"%'");
            DataRow[] drs 
= ds.Tables[0].Select("kh_jc like '"+kh_jc+"%'");
            
if(drs != null)
            
{
                
string[] result = new string[drs.Length];
                
for(int i=0;i<drs.Length;i++)
                    result[i] 
= Convert.ToString(drs[i]["kh_jc"]);
                
return result;
            }

            
return null;
        }

        
/// <summary>
        
/// 读取成品的名称(聚合查询)
        
/// </summary>
        
/// <param name="cp_name"></param>
        
/// <returns></returns>

        [Ajax.AjaxMethod()]
        
public static string[] getC_CP2_name(string cp_name)
        
{
            StringBuilder strSql
=new StringBuilder();
            strSql.Append(
"select kh_jc from c_cp2 ");
            strSql.Append(
" where CP_NAME like %");
            strSql.Append(cp_name);
            strSql.Append(
"");
            strSql.Append(
"group BY CP_NAME ");

            DataSet ds 
= INC.DB.Query(strSql.ToString());
            DataRow[] drs 
= ds.Tables[0].Select("cp_name like '"+cp_name+"%'");
            
if(drs != null)
            
{
                
string[] result = new string[drs.Length];
                
for(int i=0;i<drs.Length;i++)
                    result[i] 
= Convert.ToString(drs[i]["cp_name"]);
                
return result;
            }

            
return null;
        }


    }

}

. 新建自己的Web项目文件。可以参考ch7.2_student或ch8.2_goods案例中的相关代码,但应尽量自己原创,不是原创将根据情况扣分。 2. 在数据库中新建数据表,增添若干条记录。 3. 自己新建若干网页,分别实现数据的列表、编辑、新添、删除等功能。 4. 用户登录部分可以参考或直接借用ch7.2_student或ch8.2_goods已有源代码。Db.java文件、MD5.java文件、VerifyCode.java文件、CSS文件、富文本编辑器UEditor和图片上传功能等也可以直接使用。 5. 需用到Servlet(如验证码、Ajax或MVC编程中的控制器类等)、表单控件(除了文本框,需包含单选按钮、复选框、下拉列表等控件中的一种或多种)。 6. 如果自己原创案例,或使用MVC模式编程实现案例,得分将更高。 三、建议的网站建设流程: (1)创建数据表tb_ Cba,录入几条记录; (2)创建实体类bean/Cba.java; (3)创建数据操作类dao/CbaDao.java,实现selectCount()、selectAll()方法 (4)创建业务service/CbaService.java,实现selectAll()方法 (5)创建控制servlet/CbaList.java (6)创建列表页面cba/cbaList.jsp (7)实现列表页之后,再类似地实现详情、新添、修改和删除功能。 四、特别强调 1. 在数据库中,除了用户表tb_user,学生需自创数据表和记录,自创的数据表的名称、字段名称、字段个数和字段值,需与ch7.2_student或ch8.2_goods中的明显不同。否则判答辩不及格。 不许出现的表名和字段名:tb_student(共8个字段,其中studentId,studentNo,studentName,className不许出现 ),tb_goods(共11个字段,其中goodsId,goodsNo,goodsName,typeId,typeName不许出现 ),tb_type(共4个字段,其中typeId,typeName不许出现 )。 2. 在实现数据的列表、编辑、新添、删除功能的文件名和源代码中,除了与用户登录相关的文件名和源代码,其它的文件名、变量名和变量个数,需与ch7.2_student或ch8.2_goods中的明显不同。否则判答辩不及格。 在项目中不许出现的变量名称:student,studentId,studentNo,studentName,className,goods,goodsId,goodsNo,goodsName,type,typeId,typeName,在文件名称中也不允许包含以上名称。 3. 各同学选择的素材不许相同。 宠物商城管理
最新发布
06-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值