IronPython getting started the second article uses shared code to create a simple IronPython class and use it on the ASP. NET page.
1. Create a Web site and ASP. NET page, and select IronPython as the language.
2. About the App_Script folder. After the first step, a folder named App_Script is automatically created in the new Web site. Here you can add reusable IronPython sharing classes, you can only put classes in this folder, but not other files such as Web Page and User Control.
3. Create a simple shared class. Add a new item in the App_Script folder. The following dialog box is displayed. Select IronPython Module.
Create a simple shared class SampleClass, which has a property TestString. It uses the property () function to specify its access methods SetTestString () and GetTestString (). The Code is as follows:
Class SampleClass:
"Sample class with one property"
_ TestString = ""
Def SetTestString (value ):
_ TestString = value
Def GetTestString ():
Return _ testString
TestString = property (GetTestString, SetTestString)
4. Use the shared class to add related controls on the ASP. NET page, as shown below:
<Div>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox> & nbsp;
<Asp: Button ID = "Button1" runat = "server" Text = "Button" OnClick = "button#click"/> <br/>
<H3> <asp: Label ID = "Label1" runat = "server" Text = "Label"> </asp: Label>
</Div>
Open the Default. aspx. py file and import the namespace:
Import SampleModule
From SampleModule import SampleClass
Click events for the edit button:
Def button#click (sender, args ):
SC = SampleClass ()
SC. TestString = TextBox1.Text
Label1.Text = SC. TestString
After running, enter TerryLee in the text box and click the following button:
Full sample code download: http://files.cnblogs.com/Terrylee/IronPythonDemo2.rar
Note: This example is from the IronPython getting started tutorial.