Whidbey中客戶端回調機制(三)
2024-07-21 02:16:26
供稿:網友
第二個參數(shù) (context) 是與country下拉框同步統(tǒng)一。(我將會稍后在這個文章中解釋在去限器和那context參數(shù)的使用)。 如果你重新請求, 服務器端 raisecallbackevent 仍然執(zhí)行。在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>
處理回調
現(xiàn)在處理回調,你需要增加兩種客戶端處理回調的方法: 一是callbackhandler 處理成功的回調和一種errorcallback 處理任何錯誤的方法。 listing 4顯示了腳本的定義。 注意兩種方法有相似的簽名。
callbackhandler 方法的result參數(shù)將從raisecallbackevent方法那里返回限定的string列表, 并且context參數(shù)將適當?shù)目刂苅d(在listing 2中,與callserver相同方法的第2 參數(shù), 你傳遞country的"id"作為一個context參數(shù))。 注意如果(context == "cbocountry") ...使用context得到對合適的形式。所有殘余處理返回價,使用split獲得dropdown 目錄的一個矩陣。
錯誤處理
在遠程回調期間出現(xiàn)的錯誤通過callbackmanager發(fā)送回叫到errorcallback方法。listing 1顯示了errorcallback的方法。errorcallback 方法只包含在客戶端的警告信息。你也可以包括更多的錯誤處理。result參數(shù)提供錯誤信息。為了測試,可以從服務器邊事件拋出一些異常, 然后檢查其值。
可以運行這個例子來查看callback是如何工作的。如果你準備運行這個例子,解壓縮文件到一個文件夾,建立虛擬目錄。在運行之前一定要更新修改web.config文件中connectionstring的值。
作者提示: 并非全部瀏覽器都支持回叫的實施。 可以使用httpbrowsercapabilities類并檢查兩個新bool 屬性的返回價值: supportscallback和supportsxml http。 返回值為true表明瀏覽器支持callback。
與遠程腳本相比較,在asp.net v2.0里的回叫能力是一個更好的實施模型。 你可以實現(xiàn)回叫來返回數(shù)據(jù)查找,產生backend 事件,或者允許用戶輸入值。 使用xml 能使你的回叫更加強有力。 你能非常有效地使用對錯誤回叫的支持來驗證用戶輸入。 最大的優(yōu)點就是使用戶與全頁的postback/redraw 循環(huán)的分離。
本文來源于網頁設計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。