c# - 为什么我不能在 ASP.NET 网站上使用部分类?

标签 c# asp.net

我必须处理一个网站项目并且需要使用部分类。但使用时出现问题。

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 ​​当前上下文中不存在名称“Salary”

最佳答案

您需要在调用方声明 Test() 方法。使其公开也许是可能的,但似乎不是一个好主意。

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
    }
}

关于c# - 为什么我不能在 ASP.NET 网站上使用部分类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20346345/

相关文章:

c# - GridView 排序将页面重置为原始数据

c# - User.IsInRole 每次都会在 Active Directory 中查找吗?

c# - 反射器和编译器生成的代码

c# - 在 C#/ASP.Net 中用分隔符分割字符串

c# - sql ExecuteNonQuery 不更新 C# 中的数据库

C# 和 C++ 库

c# - ASP :RadioButtonList add RadioButton to new line

ASP.NET 模拟 NT AUTHORITY\IUSR 但模拟被禁用。 ASP.NET 错误?

c# - Sharepoint 访问请求 "Permission"代码

c# - 如何将 Json 数组反序列化为不同的对象?