现在的位置: 首页 > 综合 > 正文

IronPython 也玩 Ajax!

2012年09月23日 ⁄ 综合 ⁄ 共 1019字 ⁄ 字号 评论关闭
在 IronPython 搭建的项目中(也可以是和 C# 的混合项目,详见我前一篇 post),可以使用 Anthem.NET 来轻松实现 Ajax 功能。

下面我简单的演示一个例子:在页面上我们分别放一个 Anthem 的文本框和按钮控件,点击按钮时,用异步回调的方式更改文本框中的值。
代码很简单:

页面 ajax1.aspx:

<%@ Page Language="IronPython" CodeFile="ajax1.aspx.py" %>
<%@ Register Assembly="Anthem" TagPrefix="anthem" Namespace="Anthem" %>

<!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>IronPython 的 Ajax 例子</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
      
<anthem:TextBox ID="txt1" runat="server" />
      
<anthem:Button ID="btn1" runat="server" Text="测试" AutoCallBack="true" PreCallBackFunction="" OnClick="btn1_Click" />      
    
</div>
    
</form>
</body>
</html>

后台代码 ajax1.aspx.py:

def btn1_Click(sender, args):
    txt1.Text 
= u"中国人"
    txt1.UpdateAfterCallBack 
= True

演示效果:

ipyajax1.jpg

例子虽然非常简单,但在目前来看,可以表明 IronPython for ASP.NET 的实现有着无限的应用可能 :)

上述例子中需要注意的是,IronPython 的源代码中字符串如果包含中文,则需要用 u'字符串' 的方式来表示,否则会失败。并且这个代码文件需要保存为 UTF-8 格式。

抱歉!评论已关闭.