ASP.NET中AJAX 調(diào)用實(shí)例代碼
更新時(shí)間:2012年05月03日 19:31:46 作者:
最近在ASP.NET中做了一個(gè)AJAX調(diào)用 : Client端先從ASP.NET Server后臺(tái)取到一個(gè)頁面模板,然后在頁面初始化時(shí)再從Server中取一些相關(guān)數(shù)據(jù)以實(shí)現(xiàn)頁面模板的動(dòng)態(tài)顯示
1前言
最近在ASP.NET中做了一個(gè)AJAX調(diào)用 : Client端先從ASP.NET Server后臺(tái)取到一個(gè)頁面模板,然后在頁面初始化時(shí)再從Server中取一些相關(guān)數(shù)據(jù)以實(shí)現(xiàn)頁面模板的動(dòng)態(tài)顯示。具體實(shí)現(xiàn)為:
1) Client向 ASP.NET后臺(tái)發(fā)送HTTP GET 請(qǐng)示
2) 后臺(tái)給Client發(fā)送一個(gè)HTML模板,同時(shí)在內(nèi)存中存儲(chǔ)一個(gè)XML String (包含頁面模板動(dòng)態(tài)顯示所需的數(shù)據(jù))
3) Client在初始化頁面時(shí),發(fā)送AJAX請(qǐng)求,拿到XML String
4) 利用拿到的XML String,定制化HTMl模板,實(shí)現(xiàn)HTML頁面模板的動(dòng)態(tài)顯示。
2幾個(gè)關(guān)鍵點(diǎn)的簡介與代碼示例
2.1 ASP.NET Server端
2.1.1 用C#生成XML String
用System.Xmlnamespace下的幾個(gè)類就可以實(shí)現(xiàn)。下面是Code sample,
ArrayList steps = new ArrayList();
String errordiscription = "Not in position";
for (int i = 0; i < 5; i++)
{
steps.Add(new Step(@"images/1.jpg", "step21 description"));
}
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
//add the root
XmlNode rootNode = doc.CreateElement("Root");
doc.AppendChild(rootNode);
//add the error description node
XmlNode errorNode = doc.CreateElement("ErrorDescription");
errorNode.AppendChild(doc.CreateTextNode(errordiscription));
rootNode.AppendChild(errorNode);
//add the steps node
XmlNode productsNode = doc.CreateElement("Steps");
rootNode.AppendChild(productsNode);
for (int i = 0; i < steps.Count; i++)
{
XmlNode productNode = doc.CreateElement("step");
XmlAttribute productAttribute = doc.CreateAttribute("description");
productAttribute.Value = ((Step)steps[i]).description;
productNode.Attributes.Append(productAttribute);
//productNode.Value = ((Step)steps[i]).imagePath;
productNode.AppendChild(doc.CreateTextNode(((Step)steps[i]).imagePath));
productsNode.AppendChild(productNode);
}
Global.Repairsteps= doc.InnerXml;
生成的XML如下:
<?xml version="1.0" encoding="UTF-8" ?>
- <Root>
<ErrorDescription>Not in position</ErrorDescription>
- <Steps>
<step description="step21 description">images/1.jpg</step>
<step description="step21 description">images/1.jpg</step>
<step description="step21 description">images/1.jpg</step>
<step description="step21 description">images/1.jpg</step>
<step description="step21 description">images/1.jpg</step>
</Steps>
</Root>
2.1.2 響應(yīng)Ajax請(qǐng)求,返回XML 流
這里就只有一點(diǎn)需要注意,加個(gè)HTML Header,聲明 Content-Type.
Response.Clear();
Response.AddHeader("Content-Type", "text/xml");
Response.Write(Global.Repairsteps);
Response.End();
2.1.3 多個(gè)Request之間數(shù)據(jù)共享
實(shí)現(xiàn)多個(gè)Request之間數(shù)據(jù)共享的方法很簡單直觀,利用一個(gè)Global對(duì)象就可以了。
public class Global
{
/// <summary>
/// Global variable storing important stuff.
/// </summary>
static string _repairsteps;
/// <summary>
/// Get or set the static important data.
/// </summary>
public static string Repairsteps
{
get
{
return _repairsteps;
}
set
{
_repairsteps = value;
}
}
}
2.2 Client端
2.2.1 AJAX獲取 XML
$.ajax({
type: "GET",
url: "http://localhost:3153/WebSite2/",
cache: false,
dataType: "xml",
error:function(xhr, status, errorThrown) {
alert(errorThrown+'\n'+status+'\n'+xhr.statusText);
},
success: function(xml) {
//Here we can process the xml received via Ajax
}});
2.2.2 動(dòng)態(tài)插入HTML List 元素
比較常見的方法是生成html stream,然后用append或html把Stream插入到DOM里面去。這樣做有一個(gè)問題,如果Stream里恰好有雙引號(hào)或單引號(hào)時(shí),就要用 很多的“\”分隔符,容易出錯(cuò),可讀性不太法,不太方便,事實(shí)上,JQuery有個(gè)create new element的功能。只要給$的輸入?yún)?shù)包含<tag ... >時(shí),JQuery就不會(huì)把它理解成一個(gè)selector expression, 而是把它理解成一個(gè)生成新的DOM element 。以下是一個(gè)code sample.
$(xml).find("step").each(function(){
var stepimagepath=$(this).text();
var stepdescription=$(this).attr("description");
additem(stepimagepath, stepdescription);
});
function additem(stepimagepath, stepdescription){
$('.ad-thumbs ul').append(
$('<li>').append(
$('<a>').attr('href', stepimagepath).append(
$('<img>').attr('src', stepimagepath).attr('alt', stepdescription)
)));
}
生成的HTML section 如下:
<ul class="ad-thumb-list">
<li><a href="images/1.jpg"><img src="images/1.jpg" alt="step21 description"></a></li>
<li><a href="images/1.jpg"><img src="images/1.jpg" alt="step21 description"></a></li>
<li><a href="images/1.jpg"><img src="images/1.jpg" alt="step21 description"></a></li>
<li><a href="images/1.jpg"><img src="images/1.jpg" alt="step21 description"></a></li>
<li><a href="images/1.jpg"><img src="images/1.jpg" alt="step21 description"></a></li>
</ul>
3總結(jié)
本文介紹了在ASP.NET中使用Ajax可能會(huì)碰到的幾個(gè)關(guān)鍵點(diǎn)。 C#生成XML流,AJAX實(shí)現(xiàn)(Server端與Client端), Global 變量,與及如果用一種比較好的方法動(dòng)態(tài)插入HTML 元素.
4參考
http://www.dotnetperls.com/global-variables-aspnet
http://api.jquery.com/jQuery/
最近在ASP.NET中做了一個(gè)AJAX調(diào)用 : Client端先從ASP.NET Server后臺(tái)取到一個(gè)頁面模板,然后在頁面初始化時(shí)再從Server中取一些相關(guān)數(shù)據(jù)以實(shí)現(xiàn)頁面模板的動(dòng)態(tài)顯示。具體實(shí)現(xiàn)為:
1) Client向 ASP.NET后臺(tái)發(fā)送HTTP GET 請(qǐng)示
2) 后臺(tái)給Client發(fā)送一個(gè)HTML模板,同時(shí)在內(nèi)存中存儲(chǔ)一個(gè)XML String (包含頁面模板動(dòng)態(tài)顯示所需的數(shù)據(jù))
3) Client在初始化頁面時(shí),發(fā)送AJAX請(qǐng)求,拿到XML String
4) 利用拿到的XML String,定制化HTMl模板,實(shí)現(xiàn)HTML頁面模板的動(dòng)態(tài)顯示。
2幾個(gè)關(guān)鍵點(diǎn)的簡介與代碼示例
2.1 ASP.NET Server端
2.1.1 用C#生成XML String
用System.Xmlnamespace下的幾個(gè)類就可以實(shí)現(xiàn)。下面是Code sample,
復(fù)制代碼 代碼如下:
ArrayList steps = new ArrayList();
String errordiscription = "Not in position";
for (int i = 0; i < 5; i++)
{
steps.Add(new Step(@"images/1.jpg", "step21 description"));
}
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
//add the root
XmlNode rootNode = doc.CreateElement("Root");
doc.AppendChild(rootNode);
//add the error description node
XmlNode errorNode = doc.CreateElement("ErrorDescription");
errorNode.AppendChild(doc.CreateTextNode(errordiscription));
rootNode.AppendChild(errorNode);
//add the steps node
XmlNode productsNode = doc.CreateElement("Steps");
rootNode.AppendChild(productsNode);
for (int i = 0; i < steps.Count; i++)
{
XmlNode productNode = doc.CreateElement("step");
XmlAttribute productAttribute = doc.CreateAttribute("description");
productAttribute.Value = ((Step)steps[i]).description;
productNode.Attributes.Append(productAttribute);
//productNode.Value = ((Step)steps[i]).imagePath;
productNode.AppendChild(doc.CreateTextNode(((Step)steps[i]).imagePath));
productsNode.AppendChild(productNode);
}
Global.Repairsteps= doc.InnerXml;
生成的XML如下:
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="UTF-8" ?>
- <Root>
<ErrorDescription>Not in position</ErrorDescription>
- <Steps>
<step description="step21 description">images/1.jpg</step>
<step description="step21 description">images/1.jpg</step>
<step description="step21 description">images/1.jpg</step>
<step description="step21 description">images/1.jpg</step>
<step description="step21 description">images/1.jpg</step>
</Steps>
</Root>
2.1.2 響應(yīng)Ajax請(qǐng)求,返回XML 流
這里就只有一點(diǎn)需要注意,加個(gè)HTML Header,聲明 Content-Type.
復(fù)制代碼 代碼如下:
Response.Clear();
Response.AddHeader("Content-Type", "text/xml");
Response.Write(Global.Repairsteps);
Response.End();
2.1.3 多個(gè)Request之間數(shù)據(jù)共享
實(shí)現(xiàn)多個(gè)Request之間數(shù)據(jù)共享的方法很簡單直觀,利用一個(gè)Global對(duì)象就可以了。
復(fù)制代碼 代碼如下:
public class Global
{
/// <summary>
/// Global variable storing important stuff.
/// </summary>
static string _repairsteps;
/// <summary>
/// Get or set the static important data.
/// </summary>
public static string Repairsteps
{
get
{
return _repairsteps;
}
set
{
_repairsteps = value;
}
}
}
2.2 Client端
2.2.1 AJAX獲取 XML
復(fù)制代碼 代碼如下:
$.ajax({
type: "GET",
url: "http://localhost:3153/WebSite2/",
cache: false,
dataType: "xml",
error:function(xhr, status, errorThrown) {
alert(errorThrown+'\n'+status+'\n'+xhr.statusText);
},
success: function(xml) {
//Here we can process the xml received via Ajax
}});
2.2.2 動(dòng)態(tài)插入HTML List 元素
比較常見的方法是生成html stream,然后用append或html把Stream插入到DOM里面去。這樣做有一個(gè)問題,如果Stream里恰好有雙引號(hào)或單引號(hào)時(shí),就要用 很多的“\”分隔符,容易出錯(cuò),可讀性不太法,不太方便,事實(shí)上,JQuery有個(gè)create new element的功能。只要給$的輸入?yún)?shù)包含<tag ... >時(shí),JQuery就不會(huì)把它理解成一個(gè)selector expression, 而是把它理解成一個(gè)生成新的DOM element 。以下是一個(gè)code sample.
復(fù)制代碼 代碼如下:
$(xml).find("step").each(function(){
var stepimagepath=$(this).text();
var stepdescription=$(this).attr("description");
additem(stepimagepath, stepdescription);
});
function additem(stepimagepath, stepdescription){
$('.ad-thumbs ul').append(
$('<li>').append(
$('<a>').attr('href', stepimagepath).append(
$('<img>').attr('src', stepimagepath).attr('alt', stepdescription)
)));
}
生成的HTML section 如下:
復(fù)制代碼 代碼如下:
<ul class="ad-thumb-list">
<li><a href="images/1.jpg"><img src="images/1.jpg" alt="step21 description"></a></li>
<li><a href="images/1.jpg"><img src="images/1.jpg" alt="step21 description"></a></li>
<li><a href="images/1.jpg"><img src="images/1.jpg" alt="step21 description"></a></li>
<li><a href="images/1.jpg"><img src="images/1.jpg" alt="step21 description"></a></li>
<li><a href="images/1.jpg"><img src="images/1.jpg" alt="step21 description"></a></li>
</ul>
3總結(jié)
本文介紹了在ASP.NET中使用Ajax可能會(huì)碰到的幾個(gè)關(guān)鍵點(diǎn)。 C#生成XML流,AJAX實(shí)現(xiàn)(Server端與Client端), Global 變量,與及如果用一種比較好的方法動(dòng)態(tài)插入HTML 元素.
4參考
http://www.dotnetperls.com/global-variables-aspnet
http://api.jquery.com/jQuery/
相關(guān)文章
jquery插件之easing 動(dòng)態(tài)菜單
jQuery Easing Plugin 是一個(gè)簡單的為對(duì)象擴(kuò)展高級(jí)屬性和選項(xiàng)的jQuery插件。2010-08-08jQuery為動(dòng)態(tài)生成的select元素添加事件的方法
下面小編就為大家?guī)硪黄猨Query為動(dòng)態(tài)生成的select元素添加事件的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08jQuery圖片左右滾動(dòng)代碼 有左右按鈕實(shí)例
這篇文章主要介紹使用jQuery實(shí)現(xiàn)圖片左右滾動(dòng)的實(shí)例,需要的朋友可以參考下。2016-06-06實(shí)例詳解jQuery Mockjax 插件模擬 Ajax 請(qǐng)求
這篇文章主要介紹了實(shí)例詳解jQuery Mockjax 插件模擬 Ajax 請(qǐng)求的相關(guān)資料,需要的朋友可以參考下2016-01-01jQuery獲得包含margin的outerWidth和outerHeight的方法
這篇文章主要介紹了jQuery獲得包含margin的outerWidth和outerHeight的方法,涉及jQuery中outerWidth及outerHeight方法的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03jquery的attr方法禁用表單元素禁用輸入內(nèi)容
這篇文章主要介紹了通過jquery的attr方法來禁用表單元素禁輸入內(nèi)容,示例如下,需要的朋友可以參考參考2014-06-06