IronPython for ASP.NET:使用共用代碼

來源:互聯網
上載者:User

IronPython入門教程第二篇使用共用代碼,建立一個簡單的IronPython類,並在ASP.NET頁面中使用它。

1.建立Web網站和ASP.NET頁面,選擇語言為IronPython。


2.關於App_Script檔案夾。經過上面第一步操作後,建立Web網站中會自動建立一個名為App_Script的檔案夾,在這裡你可以添加一些可重用的IronPython共用類,在該檔案夾下只可以放類,而不能放其它諸如Web Page,User 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,單擊按鈕如下:

完整範例程式碼下載:http://files.cnblogs.com/Terrylee/IronPythonDemo2.rar

註:該例子來自於IronPython 入門教程。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.