Extending ASP.net pages using Partial Classes

G

Guest

I'd like to extend some of my Asp.net pages by using Partial Classes.

Example ASP.Net Page:
public partial class Admin_Customer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Data_List();
}
}

I'd like to add (in another file):
public partial class Admin_Customer
{
protected void Data_List()
{ . . code . . .}
}

Each time I complie the page cannot see the Data_List() method ?
Error Message "The name Data_List does not exist in the current contact".

I've been scouring the web all morning with no sucess, tried a few things
like putting them in the same namespace but that does not seem to help. Also
given them the same inherit statement.

Any suggestions Please ?
 
J

Juan T. Llibre

You'll need to place the source file extending the class in the App_Code directory.

You cannot extend a partial class in a page's code-behind source
in a different page's code-behind source.

Try it, and view the complete class with Visual Studio's Class View feature.
 
G

Guest

Juan,

The "new" partial class is in the \App_Code\PartialClasses.cs file.

The .aspx page (and .aspx.cs) is in the root.

Any other ideas ?

Thx, Lee.
 
J

Juan T. Llibre

Are both partial classes within the same namespace ?

i.e.,

in one file :
Namespace somenamespace
public partial class Admin_Customer

in PartialClasses.cs :
Namespace somenamespace
public partial class Admin_Customer
 
J

Juan T. Llibre

Try this :

Create a new website, with a default.aspx and a default.aspx.cs.

Leave the default.aspx and the default.aspx.cs pages as they are.
Create an App_Code directory and create a source file named "Default.cs" in it.

Substitute the following code for the code in Default.cs :

----

public partial class _Default : System.Web.UI.Page
{
public _Default()
{
int m_MyProperty;}
// This is the property block.
public int MyProperty
{
// Returns the value of m_MyProperty.
get
{
return m_MyProperty;
}
set
{
// Checks if a valid value is being
// assigned to the property.
if (value > 5)
{
//
//TO DO
//
}
else
{
m_MyProperty = value;
}
}
}

----

Now, use the Class View feature in Visual Studio and highlight the _Default partial class.

You will see the property MyProperty listed for the
_Default partial class...which inherits from System.Web.UI.Page.

Congratulations!
You have just extended the partial class _Default from the source file "Default.cs".

Adapt the example to any other extension you want to add to _Default,
by adding properties or methods to Default.cs in the APp_Code directory.
 
G

Guest

Juan,

Works a treat.

However I cannot use the "MyProperty" property from the Page_Load event in
the original code behind class - this is what I want to do.

The error is "The name 'MyProperty' does not exist in the current context."

Same goes for methods.

Lee :eek:(
 
J

Juan T. Llibre

re:
Does Juan or anyone else have any ideas here ?

A solution would be to compile your helper classes (HelperClasses.cs) into an assembly and
then it will be a snap to call your added functions from HelperClasses.dll in the /bin directory.
 
G

Guest

Juan,

Thanks for you help again !

I already have a helper class DLL and was hoping to go further and generate
the matching calls for each admin page (and use the partial classes).

Guess I'll have to do the last bit by hand.

Thanks again for your help.

Lee :eek:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top