这两天也在学习写IronPython For ASP.Net 的Web程序,可是在学习的过程中,遇到一个IronPython和C#对用户控件模型的解析不一致的情况,理解不了,就写出来,请大家有兴趣的帮我解惑。
IronPython和C#都是写一个简单的用户控件,可是它们在解析用户控件的私有方法是,却不一致。
先看IronPython的。
<script runat="server">
def Page_Load():
__PrivateMethod("1")
PublicMethod("2")
def __PrivateMethod(text) :
lblPrivate.Text = "private %s" % text
def PublicMethod(text) :
lblPublic.Text = "public %s " % text
def Private_Click(sender,e):
__PrivateMethod("3")
</script>
<asp:Label ID=lblPrivate runat=server></asp:Label><br />
<asp:Label ID=lblPublic runat=server></asp:Label>
<asp:Button ID=btnPrivate OnClick="Private_Click" runat=server />
控件名是联系IronPython For ASP.Net CTP中的例程时候遗留的,就没改。
按照python的语法,类的成员的标识符如果以_打斗,那该成员就是私有的。
好,那对应的C#写的ListPicker就应该如下。
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
PrivateMethod("1");
PublicMethod("2");
}
private void PrivateMethod(string text)
{
lblPrivate.Text = "private " + text;
}
public void PublicMethod(string text)
{
lblPublic.Text = "public " + text;
}
private void Private_Click(object sender, EventArgs e)
{
PrivateMethod("Private_Click");
}
</script>
<asp:Label ID=lblPrivate runat=server></asp:Label><br />
<asp:Label ID=lblPublic runat=server></asp:Label>
<asp:Button ID=btnPrivate OnClick="Private_Click" runat=server />
同样,PrivateMethod由于有private限定,也是私有的。
现在,问题出现了. Python 版的站点页面注册引用ListPicker后,可以访问ListPicer1.__PrivateMethod,一个简单的aspx页面如下。
<%@ Register Src="ListPicker.ascx" TagName="ListPicker" TagPrefix="uc1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:ListPicker id="ListPicker1" runat="server">
</uc1:ListPicker>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></div>
</form>
</body>
</html>
与之相对应的py代码文件是:
from System.Data import *
from System.Web import *
from System.Web.UI import *
from clr import *
def Page_Load():
pass
def Button1_Click(sender,e):
ListPicker1.__PrivateMethod("ff")
该IronPython 站点运行正常。
同样的,另一个站点C#版的Default.aspx和Default.aspx.cs分别如下:
<%@ Register Src="ListPicker.ascx" TagName="ListPicker" TagPrefix="uc1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:ListPicker ID="ListPicker1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ListPicker1.PrivateMethod("3");
}
}
此C#站点程序则出现编译错误:
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0117: 'ASP.ListPicker' does not contain a definition for 'PrivateMethod'
Source Error:
Line 17: protected void Button1_Click(object sender, EventArgs e)
Line 18: {
Line 19: ListPicker1.PrivateMethod("3");
Line 20: }
Line 21:
Source File: d:\WWWForDotnet2\First\Default.aspx.cs Line: 19
下面,我的困惑是。 为什么IronPython的default.aspx.py文件里可以访问ListPicker1.__PrivateMethod?难道IronPython不是按照经典的Asp.Net控件模型来生成用户控件的? 为了验证,我把IronPython版的ListPicker控件里btnButton的Click事件def PrivateClick改成def __PrivateClick,却出现解析错误。
--------------------------------------------------------------------------------
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: The page doesn't have an event handler named 'Private_Click'
Source Error:
Line 20: <asp:Label ID=lblPrivate runat=server></asp:Label><br />
Line 21: <asp:Label ID=lblPublic runat=server></asp:Label>
Line 22: <asp:Button ID=btnPrivate OnClick="Private_Click" runat=server />
Source File: /Second/ListPicker.ascx Line: 22
按照这个错误提示来看,似乎IronPython又是按照Asp.Net经典模型来实现的。
令人费解啊。
可是还有费解的。 将Default.aspx.cs里的ListPicker1.PrivateMethod访问改成ListerPicker1.PublicMethod,让程序能运行。 同时,改动C#版的ListPicker的btnClick的事件声明为pirvate void Private_Click, btnClick控件的Click事件照样能得到响应。
再一次费解。
热切希望了解其中原因的朋友指点,不然,我可能要失眠几个晚上了。