ASP.NET中AJAX 调用实例代码 - Web前端
作者:98wpeu发布时间:2026-06-12分类:网页前端技术浏览:252
导读:1前言最近在asp.net中做了一个Ajax调用:client端先从ASP.NETServer后台取到一个页面模板,然后在页面初始化时再从Server中取一些相关数据以实现...
1前言
最近在asp.net中做了一个Ajax调用 : client端先从ASP.NET Server后台取到一个页面模板,然后在页面初始化时再从Server中取一些相关数据以实现页面模板的动态显示。具体实现为:
1) Client向 ASP.NET后台发送httpget 请示
2) 后台给ClIEnt发送一个html模板,同时在内存中存储一个xmlstring (包含页面模板动态显示所需的数据)
3) Client在初始化页面时,发送ajax请求,拿到XML String
4) 利用拿到的XML String,定制化HTML模板,实现HTML页面模板的动态显示。
2几个关键点的简介与代码示例
2.1 ASP.NET Server端
2.1.1 用c#生成XML String
用System.Xmlnamespace下的几个类就可以实现。下面是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 响应Ajax请求,返回XML 流
这里就只有一点需要注意,加个HTML header,声明 Content-type.
复制代码 代码如下:
Response.Clear();
Response.AddHeader("Content-Type", "text/xml");
Response.Write(Global.Repairsteps);
Response.End();
2.1.3 多个Request之间数据共享
实现多个Request之间数据共享的方法很简单直观,利用一个Global对象就可以了。
复制代码 代码如下:
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 动态插入HTML List 元素
比较常见的方法是生成html Stream,然后用append或html把Stream插入到DOM里面去。这样做有一个问题,如果Stream里恰好有双引号或单引号时,就要用 很多的“\”分隔符,容易出错,可读性不太法,不太方便,事实上,JQuery有个create new element的功能。只要给$的输入参数包含<tag ... >时,jQuery就不会把它理解成一个selector Expression, 而是把它理解成一个生成新的dom element 。以下是一个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总结
本文介绍了在ASP.NET中使用Ajax可能会碰到的几个关键点。 C#生成XML流,AJAX实现(Server端与Client端), Global 变量,与及如果用一种比较好的方法动态插入HTML 元素.
4参考
http://www.dotnetperls.com/global-variables-aspnet
http://api.jquery.com/jQuery/
最近在asp.net中做了一个Ajax调用 : client端先从ASP.NET Server后台取到一个页面模板,然后在页面初始化时再从Server中取一些相关数据以实现页面模板的动态显示。具体实现为:
1) Client向 ASP.NET后台发送httpget 请示
2) 后台给ClIEnt发送一个html模板,同时在内存中存储一个xmlstring (包含页面模板动态显示所需的数据)
3) Client在初始化页面时,发送ajax请求,拿到XML String
4) 利用拿到的XML String,定制化HTML模板,实现HTML页面模板的动态显示。
2几个关键点的简介与代码示例
2.1 ASP.NET Server端
2.1.1 用c#生成XML String
用System.Xmlnamespace下的几个类就可以实现。下面是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 响应Ajax请求,返回XML 流
这里就只有一点需要注意,加个HTML header,声明 Content-type.
复制代码 代码如下:
Response.Clear();
Response.AddHeader("Content-Type", "text/xml");
Response.Write(Global.Repairsteps);
Response.End();
2.1.3 多个Request之间数据共享
实现多个Request之间数据共享的方法很简单直观,利用一个Global对象就可以了。
复制代码 代码如下:
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 动态插入HTML List 元素
比较常见的方法是生成html Stream,然后用append或html把Stream插入到DOM里面去。这样做有一个问题,如果Stream里恰好有双引号或单引号时,就要用 很多的“\”分隔符,容易出错,可读性不太法,不太方便,事实上,JQuery有个create new element的功能。只要给$的输入参数包含<tag ... >时,jQuery就不会把它理解成一个selector Expression, 而是把它理解成一个生成新的dom element 。以下是一个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总结
本文介绍了在ASP.NET中使用Ajax可能会碰到的几个关键点。 C#生成XML流,AJAX实现(Server端与Client端), Global 变量,与及如果用一种比较好的方法动态插入HTML 元素.
4参考
http://www.dotnetperls.com/global-variables-aspnet
http://api.jquery.com/jQuery/
相关推荐
- WordPress 应用:如何修改模版头部文件中的 wp_head() - Web前端
- SEO 优化:YouTube SEO Plugin 提升影片流量 - Web前端
- 使用 React 服务器组件在 Next.js 13 中进行国际化 - Web前端
- 3 个意想不到的技术 SEO 问题示例(以及如何解决它们) - Web前端
- Vue 技术专题:Suspense、路由模式与单元测试实践 - Web前端
- 如何防止 WordPress SQL 注入攻击? - Web前端
- SEO 优化:如何围绕技术SEO框架以构建和制定策略? - Web前端
- SEO优化:YouTube SEO 关键词研究工具 - Web前端
- 2025 年帮助代理机构提升客户业绩的 14 款最佳 SEO 工具 - Web前端
- 客户端:服务器之间设置本地 DNS 服务器 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 9jQuery编写widget的一些技巧分享 - Web前端
- 最近发表
-
- WordPress 应用:如何修改模版头部文件中的 wp_head() - Web前端
- SEO 优化:YouTube SEO Plugin 提升影片流量 - Web前端
- 使用 React 服务器组件在 Next.js 13 中进行国际化 - Web前端
- 3 个意想不到的技术 SEO 问题示例(以及如何解决它们) - Web前端
- Vue 技术专题:Suspense、路由模式与单元测试实践 - Web前端
- 如何防止 WordPress SQL 注入攻击? - Web前端
- SEO 优化:如何围绕技术SEO框架以构建和制定策略? - Web前端
- SEO优化:YouTube SEO 关键词研究工具 - Web前端
- 2025 年帮助代理机构提升客户业绩的 14 款最佳 SEO 工具 - Web前端
- 客户端:服务器之间设置本地 DNS 服务器 - Web前端


