ASP.NET 提供的CaLLBack回调技术实现页面无刷新

本文介绍如何利用ASP.NET 2.0的CallBack技术实现页面局部无刷新更新,通过客户端JavaScript与服务器端C#代码交互,根据输入的城市名称动态填充下拉列表。

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

.NET 2.0提供的CallBack技术通常用在自定义的控件内,这样可以实现某控件的自动回调技术

以下代码在VS2005中经过调试,可以直接运行。

Default.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 runat="server">
    
<title>CaLLBack回调技术实现页面无刷新</title>
</head>
<script language=javascript>
function filldata()
{
    
var city=document.getElementById("TextBox1").value;
    
//调用方法GetCallbackEventReference,获取服务器端返回的结果。语法如下:
    //第一个参数:表示处理客户端回调的服务器控件。该控件必须实现ICallbackEventHandler接口
                  //本例中的this表示调用回调方法的控件就是本页
    //第二个参数:从客户端脚本传递给服务器端的一个参数,应用在RaiseCallbackEvent方法中
    //第三个参数:一个客户端处理程序的名称,该处理程序接收服务器端事件的返回结果
    //第四个参数:启动回调之前在客户端计算的客户端脚本,又称脚本上下文。脚本的结果传回客户端事件处理程序
    <%=Page.ClientScript.GetCallbackEventReference(this,"city","filldll",null%>
}

function filldll(strcity)
{
    document.getElementById(
"DropDownList1").options.length=0;
    
var indexofcity;
    
var city;
    
while(strcity.length>0)
    
{
        indexofcity
=strcity.indexOf(",");
        
if(indexofcity>0)
        
{
            city
=strcity.substring(0,indexofcity);
            strcity
=strcity.substring(indexofcity+1);
            document.getElementById(
"DropDownList1").add(new Option(city,city));
        }

        
else
        
{
             document.getElementById(
"DropDownList1").add(new Option(strcity,strcity));
             
break;
        }

    }
;
}

</script>
<body>
    
<form id="form1" runat="server"> <div>
        
<table>
            
<tr>
                
<td colspan="2">
                    使用回调技术实现局部刷新
</td>
            
</tr>
            
<tr>
                
<td style="width: 217px; height: 21px">
                    输入城市的名称
</td>
                
<td style="width: 375px; height: 21px">
                    
<asp:TextBox ID="TextBox1" runat="server" Width="234px"></asp:TextBox></td>
            
</tr>
            
<tr>
                
<td style="width: 217px">
                
</td>
                
<td style="width: 375px">
                    
<input id="Button1" type="button" value="查 询"  onclick="filldata()"/></td>
            
</tr>
            
<tr>
                
<td style="width: 217px">
                    选择区域列表
</td>
                
<td style="width: 375px">
                    
<asp:DropDownList ID="DropDownList1" runat="server" Width="246px">
                    
</asp:DropDownList></td>
            
</tr>
        
</table>
    
    
</div>
    
</form>
</body>
</html>

后台代码如下:

 

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, ICallbackEventHandler
{
    
private string _data;

    
ICallbackEventHandler 成员
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值