我必须处理一个网站项目并且需要使用部分类。但使用时出现问题。
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/