ajax无刷新技术

下面是个例子:

public partial class atlas_CallBackByPrama : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler 
 2{
 3    public string _CallbackResult = null;
 4
 5    protected void Page_Load(object sender, EventArgs e)
 6    {
 7        //定义客户端的参数
 8        string cbReferenc = Page.ClientScript.GetCallbackEventReference(this"arg""getUserInfoFromServer""context");
 9
10        //定义客户端的脚本
11        string cbScript = "function UserCallBackInfo(arg,context)"
12        + "{" + cbReferenc + ";}";
13
14        //注册客户端脚本
15        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UserCallBack", cbScript, true);
16
17    }

18
19
20    //查询函数,简单写了一下没有异常处理
21    private void strUserInfo(string strInVale)
22    {        
23        //数据连接
24        SqlConnection myConn = new SqlConnection("server=(local);database=pubs;uid=sa;pwd=");
25        myConn.Open();
26
27        //数据库commadn
28        SqlCommand myCmd = new SqlCommand("select * from authors where au_lname like '%" + strInVale + "%'", myConn);
29
30        //声明DataReader对象
31        SqlDataReader dr = null;
32
33        //读取数据
34        dr = myCmd.ExecuteReader();
35
36        if (dr.Read())
37        {
38            _CallbackResult = dr["au_lname"].ToString() + "-" + dr["au_lname"].ToString() + " 电话:" + dr["phone"].ToString(); 
39        }

40
41        //关闭对象
42        dr.Close();
43
44        myConn.Close();     
45    }

46
47    #region ICallbackEventHandler 成员
48    //返回客户端结果
49    public string GetCallbackResult()
50    {
51        return _CallbackResult;        
52        //throw new Exception("The method or operation is not implemented.");
53    }

54    //回应客户端事件
55    public void RaiseCallbackEvent(string eventArgument)
56    
61
62    #endregion

63}

64
客户代码:

< head  runat ="server" >
    
< title > 参数(CallBack)样例 </ title >
    
< script  type ="text/javascript"   >
    
//提取服务端值
    function getUserInfo()
    
{
        
var vKey = document.form1.tbSearchKey.value;
        
//服务端定义的函数,这里加了参数
        UserCallBackInfo(vKey,"");
    }

    
    
//客户端赋值
    function getUserInfoFromServer(arg,context)
    
{
        document.form1.tbUserInfo.value 
= arg;
    }

        
    
</ script >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        输入您要查询的名字,如 White、Green
< br  />
        
< asp:TextBox  ID ="tbSearchKey"  runat ="server" ></ asp:TextBox >
        
< input  id ="Button1"  type ="button"  value ="查 询"  onclick ="getUserInfo()"   />< br  />
        查询结果是:
< br  />
        
< asp:TextBox  ID ="tbUserInfo"  runat ="server"  Width ="278px" ></ asp:TextBox >
        
</ div >
    
</ form >
</ body >

样例代码下载: 带参样例下载  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值