htcdmrl
htcdmrl

Reputation: 194

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

<%@ 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;
    }
}

Error:

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

Upvotes: 2

Views: 1869

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273274

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

Upvotes: 3

Related Questions