asp.net2.0 javascript 回调服务器端方法

想必很多程序员在开发web应用的时候需要在某个控件在按下回车键时,根据输入的字查询相应的值并且绑定到控件上,可能有些网友要说了,添加个button 在button事件里添加处理方法不就行了。

这种方法固然可以,但你有没有想到,如果你的这个界面有3-4个类似的功能,是不是每进行一次查询就要添加一个button,每点击一button 页面提交一次,页面刷新一次,想想如果你作为客户来使用你会有什么感想。今天给大家展示 javascript 回调服务器端方法,就可以通过javascript来调用服务器端方法(我个人的理解),既可以保证功能的实现、又能方便客户、还可以不让页面刷新。这样的好东东,如果不使用真是可惜。

下面是我写的一个简单的例子,希望给初学者一点启发。

aspx文件

<% @ 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  id ="Head1"  runat ="server" >
    
< title > 无标题页 </ title >
    
< script  language ="javascript"  type ="text/javascript" >
        
function OnCallback(strUserInfo,context)
        
{
           
// result.innerText=strUserInfo;
            document.getElementById('TextBox1').value=strUserInfo;
            
var stt=strUserInfo;
            document.getElementById(
'TextBox2').value=stt.split("-")[0];
            document.getElementById(
'TextBox3').value=stt.split("-")[1];
            document.getElementById(
'TextBox4').value=stt.split("-")[2];
          
            
        }

    
</ script >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
        
< asp:Label  ID ="Label1"  runat ="server"  Width ="130px" ></ asp:Label >
        
< input  id ="txtname"  type ="text"  onkeypress ="if(event.keyCode==13){<%=ClientScript.GetCallbackEventReference(this," document.form1.txtname.value","OnCallback",null) % > }"/> < br  />
       
        
< div   id ="result" style ="z-index: 101; left: 100px; width: 187px; position: absolute; top: 100px;
            height: 86px"
>
        
</ div >
        
< asp:TextBox  ID ="TextBox1"  runat ="server" ></ asp:TextBox >
        
< asp:TextBox  ID ="TextBox2"  runat ="server" ></ asp:TextBox >
        
        
< asp:TextBox  ID ="TextBox3"  runat ="server" ></ asp:TextBox >
        
< asp:TextBox  ID ="TextBox4"  runat ="server" ></ asp:TextBox >
    
</ form >
</ body >
</ html >

 

.cs文件

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
    
protected String strUserInfo;
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
string ICallbackEventHandler.GetCallbackResult()
    
{
        
return strUserInfo;
    }

    
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    
{
        strUserInfo 
= "Hello World" + "-"+eventArgument+"-"+"PPQQCT";
    }

}

代码中红色表示的地方需要注意

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值