繁体   English   中英

为什么我不能在asp.net网站上使用局部类?

[英]Why i can not use partial classes on asp.net website?

我必须处理一个网站项目,并且需要使用局部类。 但是使用存在问题。

TestPartial.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPartial.aspx.cs" Inherits="TestPartial" %>

<!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></title>
</head>
<body>
   <form id="form1" runat="server">
   <div>

   </div>
   </form>
</body>
</html>

TestPartial.aspx.cs

using System;

public partial class TestPartial : System.Web.UI.Page
{
    public int price = 200;

    partial void Salary();

    protected void Page_Load(object sender, EventArgs e)
    {
       Salary();

       int newPrice = price;
    }
}

TestPartial2.aspx.cs

using System;

public partial class TestPartial : System.Web.UI.Page
{
    partial void Salary()
    {
        price = 400;
    }
}

错误:

错误1名称'工资'在当前上下文中不存在

您需要在调用方声明Test()方法。 将其public可能是可行的,但似乎不是一个好主意。

public partial class TestPartial : System.Web.UI.Page
{    
    partial void Test(); // add this line

    protected void Page_Load(object sender, EventArgs e)
    {
        Test();
    }
}


public partial class TestPartial : System.Web.UI.Page
{
    partial void Test()  
    {
         // executed from Page_Load
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM