简体   繁体   English

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

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

i have to work with a website project and need to use partial classes. 我必须处理一个网站项目,并且需要使用局部类。 But there is a problem with using. 但是使用存在问题。

TestPartial.aspx 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 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 TestPartial2.aspx.cs

using System;

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

Error: 错误:

Error 1 The name 'Salary' does not exist in the current context 错误1名称'工资'在当前上下文中不存在

You need to declare the Test() method on the calling side. 您需要在调用方声明Test()方法。 Making it public may be possible but does not seem a very good idea. 将其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