IronPython for ASP.NET:使用共享代码

IronPython入门教程第二篇使用共享代码,创建一个简单的IronPython类,并在ASP.NET页面中使用它。

1.创建Web站点和ASP.NET页面,选择语言为IronPython

2.关于App_Script文件夹。经过上面第一步操作后,新建Web站点中会自动创建一个名为App_Script的文件夹,在这里你可以添加一些可重用的IronPython共享类,在该文件夹下只可以放类,而不能放其它诸如Web PageUser Control等文件。

3.创建一个简单的共享类。在App_Script文件夹中添加新项,会弹出如下对话框,选择IronPython Module。

创建一个简单的共享类SampleClass,在该类中有一个属性TestString,它通过property()函数来指定它的访问方法SetTestString()GetTestString(),代码如下:

class SampleClass:

    
"Sample class with one property"

    _testString 
= ""

    
def SetTestString(value):

        _testString 
= value


    
def GetTestString():

        
return _testString

    TestString 
= property(GetTestString, SetTestString)

4.使用共享类,在ASP.NET页面中添加相关的控件,如下所示:

<div>

    
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> &nbsp;

    
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br /><br />

    
<h3><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></h3>

</div>

打开Default.aspx.py文件,导入命名空间:

import SampleModule

from SampleModule import SampleClass

编写按钮的单击事件:

def Button1_Click(sender, args):

    sc 
= SampleClass()

    sc.TestString 
= TextBox1.Text

    Label1.Text 
= sc.TestString

运行后在文本框中输入TerryLee,单击按钮如下:

完整示例代码下载:https://files.cnblogs.com/Terrylee/IronPythonDemo2.rar

注:该例子来自于IronPython 入门教程。

原文地址:https://www.cnblogs.com/Terrylee/p/Using_Shared_Code_with_IronPython_for_ASPNET.html