asp.net 中实现录入时提示 (web service + js 实现 )

  效果如下...

demo.jpg


调用页面

 
None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< TITLE ></ TITLE >
None.gif        
< META  content ="Microsoft Visual Studio 7.0"  name ="GENERATOR" >
ExpandedBlockStart.gifContractedBlock.gif        
< SCRIPT  ID ="clientEventHandlersJS"  LANGUAGE ="javascript" > dot.gif
InBlock.gif
<!--
InBlock.gif
var index;
ExpandedSubBlockStart.gifContractedSubBlock.gif
function window_onload() dot.gif{
InBlock.gif    div1.style.left
=Text1.offsetLeft;
InBlock.gif    div1.style.top
=Text1.offsetTop+24;
InBlock.gif    div1.style.width
=Text1.offsetWidth;
InBlock.gif    index
=0;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
function Text1_onkeyup()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
InBlock.gif    
switch (window.event.keyCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
case 13
InBlock.gif            div1.style.visibility
="hidden";
InBlock.gif            
break;
InBlock.gif        
case 40:
InBlock.gif        
case 39
InBlock.gif            
if (index < div1.childNodes.length) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                index
=index+1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
for (i=1;i<=div1.childNodes.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (i==index)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Text1.value
=div1.childNodes[index-1].innerText.substr(1);
InBlock.gif                    div1.childNodes[index
-1].style.backgroundColor="lightblue";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    div1.childNodes[i
-1].style.backgroundColor="white";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
break;
InBlock.gif        
case 38:
InBlock.gif        
case 37
InBlock.gif            
if (index>1
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                index
=index-1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
for (i=1;i<=div1.childNodes.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (i==index)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Text1.value
=div1.childNodes[index-1].innerText.substr(1);
InBlock.gif                    div1.childNodes[index
-1].style.backgroundColor="lightblue";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    div1.childNodes[i
-1].style.backgroundColor="white";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
break;
InBlock.gif        
default:
InBlock.gif            index
=0;
InBlock.gif        
InBlock.gif            div1.innerHtml 
="";
InBlock.gif            docSubmit 
= new ActiveXObject("MSXML2.DOMDocument");
InBlock.gif            docSubmit.async 
= false;
InBlock.gif            docSubmit.load(
"http://localhost/ASPTips2/CH1/SearchTitles.asmx/doSearch?keyword=" + Text1.value);
InBlock.gif            
InBlock.gif            docSubmit.loadXML(docSubmit.xml.replace(
/&lt;/g,"<").replace(/&gt;/g,">"));
InBlock.gif            
InBlock.gif            
//
InBlock.gif
            var s;
InBlock.gif            s
="";
InBlock.gif            nodeList
=docSubmit.documentElement.getElementsByTagName("Table");
InBlock.gif            
for (i=0;i<nodeList.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                s
=+ "<div style=\"WIDTH:490px;\" class=\"gotTitle\" οnmοuseοver=\"javascript:this.style.backgroundColor='lightblue'\" οnmοuseοut=\"javascript:this.style.backgroundColor='white'\" οnclick=\"return gotTitle_onclick()\">&nbsp;" + nodeList(i).selectSingleNode("title").text + "</div>";
ExpandedSubBlockEnd.gif            }

InBlock.gif            div1.innerHTML
=s;
InBlock.gif            div1.style.visibility
="visible";
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
function gotTitle_onclick()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
if (window.event.srcElement.className="gotTitle")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Text1.value
=window.event.srcElement.innerText.substr(1);
InBlock.gif        div1.style.visibility
="hidden";
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
function gotTitle_onblur()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    div1.style.visibility
="hidden";
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//-->
ExpandedBlockEnd.gif
        
</ SCRIPT >
ExpandedBlockStart.gifContractedBlock.gif        
< style > dot.gif  .list{dot.gif}{ BORDER-RIGHT:black 1px solid; BORDER-TOP:black 1px outset; BORDER-BOTTOM:black 1px solid; BORDER-LEFT:black 1px outset; FONT-SIZE:x-small; VISIBILITY:hidden; WIDTH:490px; HEIGHT:20px; CURSOR:default; POSITION:absolute; }
ExpandedBlockEnd.gif    
</ style >
None.gif    
</ HEAD >
None.gif    
< BODY  LANGUAGE ="javascript"  onload ="return window_onload()" >
None.gif        
< P >< FONT  face ="宋体" > 搜索图书: </ FONT >   < INPUT  id ="Text1"  type ="text"  name ="Text1"  size ="78"  LANGUAGE ="javascript"  onkeyup ="return Text1_onkeyup()" > &nbsp; </ P >
None.gif        
< span  id ="div1"  onblur ="return gotTitle_onblur()"  class ="list" ></ span >
None.gif        
< P >< FONT  face ="宋体" ></ FONT ></ P >
None.gif    
</ BODY >
None.gif
</ HTML >


webservice


None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Diagnostics;
None.gif
using  System.Web;
None.gif
using  System.Web.Services;
None.gif
using  System.Configuration;
None.gif
None.gif
namespace  Ch2
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for SearchTitles.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SearchTitles : System.Web.Services.WebService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public SearchTitles()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InBlock.gif
            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.Data.SqlClient.SqlConnection sqlConnection1;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Component Designer generated code#region Component Designer generated code
InBlock.gif        
InBlock.gif        
//Required by the Web Services Designer 
InBlock.gif
        private IContainer components = null;
InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
InBlock.gif            
// 
InBlock.gif            
// sqlConnection1
InBlock.gif            
// 
InBlock.gif
            this.sqlConnection1.ConnectionString = "workstation id=\"gw\";packet size=4096;user id=sa;data source=\"gw\";pe" +
InBlock.gif                
"rsist security info=True;initial catalog=pubs;password=";
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Clean up any resources being used.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(disposing && components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                components.Dispose();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose(disposing);        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
// WEB SERVICE EXAMPLE
InBlock.gif        
// The HelloWorld() example service returns the string Hello World
InBlock.gif        
// To build, uncomment the following lines then save and build the project
InBlock.gif        
// To test this web service, press F5
InBlock.gif

InBlock.gif        [WebMethod]
InBlock.gif        
public string doSearch(string  keyword)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["pubs"]);
InBlock.gif
            SqlDataAdapter da=new SqlDataAdapter("select top 20 title_id,title from titles where title like @title",sqlConnection1);
InBlock.gif            da.SelectCommand.Parameters.Add(
new SqlParameter("@title","%"+keyword+"%"));
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            da.Fill(ds);
InBlock.gif            
return ds.GetXml();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


转自

   http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/episode.aspx?newsID=1242101

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值