欢迎光临
我们一直在努力

Whidbey中客户端回调机制(三)-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

第二个参数 (context) 是与country下拉框同步统一。(我将会稍后在这个文章中解释在去限器和那context参数的使用)。 如果你重新请求, 服务器端 raisecallbackevent 仍然执行。在listing 3 中为raisecallbackevent 事件的代码片断。

c#

listing 3: server-side handler:

this code snippet shows how the server handles client requests.

it builds a database query based on parameters sent by the client,

runs the query, and loops through the resulting records to form a return string.

public string raisecallbackevent(string eventargument)

{

// we used a char value for 20 as delimiter in javascript.

// split here using the same value, "20"

string[] actualvalue = eventargument.split(

char.convertfromutf32(20).tochararray() );

stringbuilder resultbuilder = new stringbuilder();

string query = null;

// based on argument generate the city/country

// query to return the result.

switch (actualvalue[0] )

{

case "city":

query = "select 0 as cityid, –select– as cityname " +

"union select cityid,cityname from tblcity where " +

"countryid = " +

actualvalue[1].tostring();

break;

case "country":

query = "select 0 as countryid, –select– as " +

"countryname union select countryid, countryname " +

"from tblcountry where regionid = " +

actualvalue[1].tostring() ;

break;

}

// check if the query is not null.

if (query != null)

{

datatable dtresult = new datatable();

sqlconnection conn = new sqlconnection(

system.configuration.configurationsettings.

appsettings["connectionstring"]);

sqldataadapter daresult = new sqldataadapter(query, conn);

conn.open(huajian(0x401, ));

daresult.fill(dtresult);

conn.close();

// if records exist build the option tags dynamically and

// return the string.

if (dtresult.rows.count > 0)

{

for(int rows =0;rows< } returnstring; return -1);

resultbuilder.tostring().length returnstring=

"resultbuilder.tostring().substring(0," string char(30)

be would this since character last the trim here

char.convertfromutf32(30)); + tostring() resultbuilder.append

(dtresult.rows[rows][1]. used hence row of end

char.convertfromutf32(20)); resultbuilder.append

(dtresult.rows[rows][0].tostring() rows for and

columns char(20) values. append to delimiter create { ++} ;rows>

处理回调

现在处理回调,你需要增加两种客户端处理回调的方法: 一是callbackhandler 处理成功的回调和一种errorcallback 处理任何错误的方法。 listing 4显示了脚本的定义。 注意两种方法有相似的签名。

callbackhandler 方法的result参数将从raisecallbackevent方法那里返回限定的string列表, 并且context参数将适当的控制id(在listing 2中,与callserver相同方法的第2 参数, 你传递country的"id"作为一个context参数)。 注意如果(context == "cbocountry") …使用context得到对合适的形式。所有残余处理返回价,使用split获得dropdown 目录的一个矩阵。

错误处理

在远程回调期间出现的错误通过callbackmanager发送回叫到errorcallback方法。listing 1显示了errorcallback的方法。errorcallback 方法只包含在客户端的警告信息。你也可以包括更多的错误处理。result参数提供错误信息。为了测试,可以从服务器边事件抛出一些异常, 然后检查其值。

可以运行这个例子来查看callback是如何工作的。如果你准备运行这个例子,解压缩文件到一个文件夹,建立虚拟目录。在运行之前一定要更新修改web.config文件中connectionstring的值。

作者提示: 并非全部浏览器都支持回叫的实施。 可以使用httpbrowsercapabilities类并检查两个新bool 属性的返回价值: supportscallback和supportsxml http。 返回值为true表明浏览器支持callback。

与远程脚本相比较,在asp.net v2.0里的回叫能力是一个更好的实施模型。 你可以实现回叫来返回数据查找,产生backend 事件,或者允许用户输入值。 使用xml 能使你的回叫更加强有力。 你能非常有效地使用对错误回叫的支持来验证用户输入。 最大的优点就是使用户与全页的postback/redraw 循环的分离。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Whidbey中客户端回调机制(三)-.NET教程,Asp.Net开发
分享到: 更多 (0)

相关推荐

  • 暂无文章