为什么我不能在asp.net网站上使用局部类? [英] Why i can not use partial classes on asp.net website?

查看:120
本文介绍了为什么我不能在asp.net网站上使用局部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站项目工作,需要使用部分类。但是,与使用问题。

i have to work with a website project and need to use partial classes. But there is a problem with using.

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名称'薪酬'不存在于当前上下文存在

Error 1 The name 'Salary' does not exist in the current context

推荐答案

您需要声明测试()方法主叫侧。使得它公共是可能的,但似乎没有一个很好的主意。

You need to declare the Test() method on the calling side. Making it public may be possible but does not seem a very good idea.

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

这篇关于为什么我不能在asp.net网站上使用局部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆