Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ASP.NET Generic template

0 views
Skip to first unread message

Celine

unread,
May 5, 2006, 3:10:18 AM5/5/06
to
I have a set of similar classes (c1, c2, ...).

I would like to create a template (or a page with as much abstraction
as possible) to copy/paste, and create ASPX pages to edit
(add/update/...) these classes.
A customised page for each class, beacause there are few differences
between them

On top of this (copied) template, I would define the name of the class
I want to edit.
The following generates error, but you get the idea:


public class TemplateEdit : System.Web.UI.Page
{
protected Class1 mainObject; //replace Class1 by the Object to edit
protected string mainObjectName;
protected Type mainObjectType;

//Define main object here
mainObjectName = "Company";
mainObjectType = typeof(Class1);

//Generic code, the same on all aspx pages
this.mainObject = (mainObjectType) new Object();
this.mainObject = (mainObjectType)Session[mainObjectName];
Session[mainObjectName] = this.mainObject;

private void SubmitBtn_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
//....
}

Greg Young

unread,
May 5, 2006, 6:29:39 AM5/5/06
to
I am just curious what you are trying to accomplish here .. it seems alot of
the items you discuss would be better served though inheritance than
copy/paste.

example :

public class MyPageBase : Page {
protected object mainObject; //replace Class1 by the Object to edit


protected string mainObjectName;
protected Type mainObjectType;

//etc
}

then

public class MyChildPage : MyBasePage {
}

"Celine" <pl...@letsdothatagain.com> wrote in message
news:1146813018.2...@j73g2000cwa.googlegroups.com...

Celine

unread,
May 9, 2006, 3:32:13 AM5/9/06
to
Very well,
This is part of the solution, thank you

but that still doesn't answer my question

Suppose I have a session variable I want to cast this main object to a
specific one.
I would need to write a generic type in MyPageBase

mainObject = (mainObjectType)Session[mainObjectName]; // does not
work

can NOT be translated in the customised pages (AFAIK) into
Class1 mainObject = new (Class1);
mainObjectName = "Class1";
mainObjectType = typeof(Class1);

This line would be copy/pasted in all pages instead of in MyPageBase

Greg Young

unread,
May 9, 2006, 12:39:10 PM5/9/06
to
I am not understanding this part of your question ... are you asking how you
can add a generic to your page base?

Cheers,

Greg Young


"Celine" <pl...@letsdothatagain.com> wrote in message

news:1147159933.6...@v46g2000cwv.googlegroups.com...

Celine

unread,
May 10, 2006, 2:22:03 AM5/10/06
to
My question is: How to write a base class?

Celine

unread,
May 10, 2006, 2:30:09 AM5/10/06
to
I Opted finaly for the following 'utility' (NOT a base class) to write
in it the commong functions to all similar pages:

Page_Load
BaseIsPostBack_Pre("Company");

public class _BaseForm
{
public _BaseForm(System.Web.UI.Page page)
{
((ImageButton)page.FindControl("CancelBtn")).Attributes.Add("onclick", "return
confirm('Are you sure?');");
}

1 - What worries me by passing the whole page as a parameter is whether
it is passed by value or by reference: What if I have a DataSet filled
later? Will it copy it too?

2 - Is there any alternative to the above?

Greg Young

unread,
May 10, 2006, 2:34:13 AM5/10/06
to
I gave an example of that already ..

"public class MyPageBase : Page {
protected object mainObject; //replace Class1 by the Object to edit
protected string mainObjectName;
protected Type mainObjectType;
//etc
}

then

public class MyChildPage : MyBasePage {
}"


Are you needing an example of using a generic within the base class?

Cheers,

Greg Young
MVP - C#


"Celine" <pl...@letsdothatagain.com> wrote in message

news:1147242123.0...@y43g2000cwc.googlegroups.com...

Steven Nagy

unread,
May 10, 2006, 2:37:25 AM5/10/06
to
1 - System.Web.UI.Page is a reference type so it doesn't matter if you
pass it by value or reference???

2 - Couldn't your web page inherit from _BaseForm instead?

Greg Young

unread,
May 10, 2006, 2:35:58 AM5/10/06
to
Yes just use a base class as presented ...

you can tie the event in the base and put this code in it.


"Celine" <pl...@letsdothatagain.com> wrote in message

news:1147242609....@v46g2000cwv.googlegroups.com...

Greg Young

unread,
May 10, 2006, 2:49:17 AM5/10/06
to
This should get you going, I just typed it up here quickly .. no compilation
assurances but it should be pretty close :)


public class BasePage : System.Web.UI.Page
{

private void AddJs() {
((ImageButton)this.FindControl("CancelBtn")).Attributes.Add("onclick",

"return
confirm('Are you sure?');");
}

private void Page_Load(object sender, EventArgs e) {
this.AddJs();
}

public BasePage() {
this.Load += new System.EventHandler(this.Page_Load);
}
}


then just take your page and inherit from BasePage instead of
System.Web.UI.Page.

Cheers,

Greg Young
MVP - C#

"Celine" <pl...@letsdothatagain.com> wrote in message

news:1147242609....@v46g2000cwv.googlegroups.com...

Celine

unread,
May 10, 2006, 4:23:49 AM5/10/06
to
Excellent, Thank you Greg
we are getting finally somewhere

Now, to go back to my first example
I want to pass a few 'parameters' to my Base class, mainly
mName: "The Company", for display, can contain spaces
mShortName: "Company", for coding
mType: a reference to another class

Suppose for example, I want the text on the cancel button to be "Cancel
The Company"
In the pages, I would assign
mName = "The Company"
I would add somewhere in the base class:
((ImageButton)this.FindControl("CancelBtn")).Text = "Cancel " +
mName

But what if I want to use mType, for example to cast as session
variable?
page: mType = typeof(Class1);
base:
Session[mName] = this.mainObject;
this.mainObject = (mType)Session[mName]; //does NOT work

my trouble lies in the last statement
Again, thank you for your help

Greg Young

unread,
May 10, 2006, 3:17:30 PM5/10/06
to
Make them protected variables in your base class and have the deriving class
(your page) set them in its constructor.

Cheers,

Greg Young
MVP - C#
"Celine" <pl...@letsdothatagain.com> wrote in message

news:1147249429.8...@e56g2000cwe.googlegroups.com...

Message has been deleted

Celine

unread,
May 12, 2006, 2:40:14 AM5/12/06
to
I am sorry I don't understand
Can you please provide me with an example to illustrate your point?

My problem is in this statement:
this.mainObject = (mType)Session[mName];

Celine

unread,
May 12, 2006, 5:02:40 AM5/12/06
to
((ImageButton)this.FindControl ... Does NOT always find the control

_BaseForm baseForm = new _BaseForm(this);
The above case works, because I am passing the page as a parameter, and
hence 'this' contains the control


CompanyEdit : _BaseForm
does NOT work.
I even searched in 'this' recursively, but found nothing, because
root.Controls is null or empty
FindControlRecursive(this, "CancelBtn"):

private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
return root;

foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
return t;
}
return null;
}

Greg Young

unread,
May 12, 2006, 6:10:01 AM5/12/06
to
Where are you calling it from? It depends on where in the page life cycle it
is called from whether or not things are initialized.


"Celine" <pl...@letsdothatagain.com> wrote in message

news:1147424560.8...@q12g2000cwa.googlegroups.com...

Celine

unread,
May 12, 2006, 10:51:56 AM5/12/06
to
I was calling it from the constructor,
Now it is working

Thank you

Arturo@discussions.microsoft.com Jaime Arturo

unread,
May 12, 2006, 6:53:01 PM5/12/06
to
Hi there...

I'll take a course in ASP.NET, but I'm a Macintosh user, do you know if
there's a Mac version of this software, or if is a way to use it in Macintosh
Operative System (MacOS X) since this OS use Unix programation?

Thanks.

Greg Young

unread,
May 13, 2006, 6:56:49 PM5/13/06
to
Yes there is .. it is open source and called "Mono" you can check it out
here http://www.mono-project.com/Main_Page

It works with apache through mod_mono ...

Cheers,

Greg Young
"Jaime Arturo" <Jaime Art...@discussions.microsoft.com> wrote in message
news:BF7CC023-F94D-42D1...@microsoft.com...

Celine

unread,
May 25, 2006, 6:07:54 AM5/25/06
to
I still failed to pass DataTypes as parameters, as in the following 3
examples:
this.mainObject = (mType)Session[mName];
this.mainObjectDt.AddMainObjectRow(this.DataSet_GUIDs_C1Row);
mainObject.UpdateMainObject(ref DataSet_GUIDs_C1DataTable)

Celine

unread,
May 26, 2006, 4:52:41 AM5/26/06
to
I still failed to pass DataTypes as parameters, as in the following 3
examples:
this.mainObject = (mType)Session[mName];
this.mainObjectDt.AddMainObjectRow(this.DataSet_GUIDs_C1Row);
mainObject.UpdateMainObject(ref DataSet_GUIDs_C1DataTable)

See the big picture here:
http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/e0859715a73167b0/349184b7d4ba2232?tvc=2&hl=en#349184b7d4ba2232

Steven Nagy

unread,
May 26, 2006, 7:10:16 AM5/26/06
to
>> this.mainObject = (mType)Session[mName];

Perhaps you need:

this.mainObject = (_BaseForm)Session[mName];

As long as your base form implements the required methods, and of
course this.mainObject is declared of type _BaseForm.
Otherwise interface might help, but abstract class is still better.

0 new messages