views:

523

answers:

5

I have created two partial classes for a web page.

Now i have given a defination of one function say submit() that i m calling at OnSubmit event of button.

But this function is not being called, the program doesnot compiles as it is not able to search the defination of function, which was defined in another partial class. Is it possible to call this function or i have to give defination of function in same file where i m calling it

eg

<%@ Page Language="C#" MasterPageFile="~/Master Pages/LeftMenu.master" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Web_Pages_Authentication_Login_Management_Registration" Title="Registration" StylesheetTheme="Default Theme 1"  %>

Registration.aspx.cs

public partial class Web_Pages_Authentication_Login_Management_Registration : System.Web.UI.Page
{
    private string firstName;
    private string lastName;
    private string userName;
    private string userPassword;
    private string primaryEmail;
protected void btnSubmit_Click(object sender, EventArgs e)
    {
       Display();
    }
}

Registration_Database.cs

   public partial class Web_Pages_Authentication_Login_Management_Registration 
    {
       const string dbFirstName = "@FirstName";
       const string dbLastName = "@LastName";
       const string dbUserName= "@UserName";
       const string dbUserPassword = "@UserPassword";
       const string dbPrimaryEmail = "@PrimaryEmail";  

       void Display()
          {
              firstName="XYZ"; //This variable is not accessible when i put both files in different directories
          }
    }

I am getting following error

Error   1   'Web_Pages_Authentication_Login_Management_Registration' does not contain a definition for 'Display' and no extension method 'Display' accepting a first argument of type 'Web_Pages_Authentication_Login_Management_Registration' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Administrator\Desktop\Online Test\Web Pages\Authentication\Login Management\Registration.aspx.cs 78 20 C:\...\Online Test\

Registration.aspx, Registration.aspx.cs, Registration_Database.cs are three files that are not in App_Code folder but belongs to one folder only out of which Registration.aspx.cs, Registration_Database.cs are partial classes and Registration.aspx is my design file. Plz let me know if u want to know some more info about my problem

I am not using DLL Files. No precompiled code

+1  A: 

Of course you can. Like every other method in a class (partial or not) you call static methods by ClassName.MethodName() or non-static as creating instance of the class and call instanceName.MethodName()

EDIT: Sorry I didn't see your examples at first.

That should work. Partial class is split in files but it is compiled as one class. So, the second part of the same partial class can access private members defined in the other with no problems.

Fedor Hajdu
i m trying to do that, but this function defination is not getting availabe at my button click event. What would be missing ?
Shantanu Gupta
A: 

Yes, it is possible to call a method on a (partial) class.

Partial classes are only a feature of some languages (such as C#) - they don't exist in IL, as they are compiled together into a single class.

However, it can sometimes be tricky since partial class declarations must be totally identical for this merging to happen. That includes the namespace, so this might be your problem.

As an illustration, this will result in a single class:

namespace MyNamespace
{
    public partial class MyClass { }

    public partial class MyClass { }
}

Thre resulting class is MyNamespace.MyClass. However, this will result in two different classes:

namespace MyNamespace
{
    public partial class MyClass
    {
        public void MyMethod();
    }
}

namespace MyOtherNamespace
{
    public partial class MyClass { }
}

This will produce two classes: MyNamespace.MyClass and MyOtherNamespace.MyClass. In this case, a call to this.MyMethod from MyOtherNamespace.MyClass will not work.

Mark Seemann
i m not been able to check for my page that in which namespace does it lies. How do i check it.
Shantanu Gupta
If there's no `namespace` declaration in your source code, the class most likely is define in the 'global' namespace. You can verify this by taking a look at the resulting IL in Reflector.
Mark Seemann
A: 

If I understand your question correctly, you have two files containing partial class definitions for the SAME class. Right? They are not two different classes?

If both files are for the same class, then you should make sure they are both defined in the same name space and that the class names are spelled exactly the same. If the names match then this should work.

If your partial classes define two different classes then the same rules as apply as for regular classes. To reference a metod of another class the method must be public, and you either need a reference to an instance of that class or the method must be static.

Rune Grimstad
+1  A: 

This should be fine. If you want, you can formalise things using partial methods; declare the partial stub in one file, and implement it in the other. This will quickly identify if you have two classes or one, and will highlight attempts to implement a partial method that isn't defined:

partial class Foo
{
    partial void Bar(string someArg);
    // other code that uses Bar(s)
}
partial class Foo
{
    partial void Bar(string someArg)
    {
        Console.WriteLine(someArg);
    }
}

Note that partial methods must be non-public, and can't have out or non-void return types.

Marc Gravell
+3  A: 

I think the problem you're experiencing is because your website is dynamically compiled, rather than a precompiled web application.

When the ASP.NET page is first accessed, the compiler will only look in the code behind file, and won't search all the other .cs files for possible extra code for the same class. Thus, it will never see the implementation of display(), which in turn causes the code behind file to fail to compile.

I would certainly not use a method like this for what you seem to want to do, but you could define a custom class that derives from System.Web.UI.Page and have your actual page derive from the new class. The class definition would have to be in App_Code for a dynamically compiled website.

Registration.aspx.cs:

public partial class Web_Pages_Authentication_Login_Management_Registration : MyPage
{
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Display();
    }
}

App_Code\MyPage.cs:

public class MyPage : System.Web.UI.Page
{
    protected void Display()
    {
        // ...
    }
}

In any case: you certainly shouldn't use static variables the way you are using them, this will cause problems if multiple people are using your site at the same time. Frankly, your example doesn't make much sense to me. Maybe if we had more information about what you're trying to accomplish, we could provide a best-practise way to do it.

Note that in your example, putting the second file in App_Code directory will not make it work. That would simply result in two classes with the exact same class name (provided the namespaces match of course). It really doesn't like that :)

Thorarin
I think you are going into the right direction. Can u plz elaborate more on this ."dynamically compiled, rather than a precompiled web application."
Shantanu Gupta
Deploying the .cs files with the .aspx files on the web server = dynamically compiled, vs. deploying one or more assemblies that contain all the code behind = precompiled.
Thorarin
Details added to question, plz help me out.
Shantanu Gupta
You cannot use more than one code behind file containing partial classes with dynamic compilation, so you will have to use another way.I'm still not sure what you're trying to accomplish however. Why are you defining a variable in one file and using them in another? This just obfuscates your code without any real purpose. It seems to me your `Submit()` should return an object containing firstName, lastName, etc.
Thorarin
Submit would not be returning me anything. It will just be inserting data into database. Now I got how to work out. Thx for your suggestion. Marking ur answer to be 200% correct as it really solves all the aspect that can occur in such a problem with complete solution. THx 1ce again +100 for answer
Shantanu Gupta