ASP.NET Generic template

  • Thread starter Thread starter Celine
  • Start date Start date
C

Celine

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)
{
//....
}
 
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 {
}
 
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
 
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
 
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?
 
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#
 
Yes just use a base class as presented ...

you can tie the event in the base and put this code in it.
 
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?
 
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#
 
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
 
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#
 
I am sorry I don't understand
Can you please provide me with an example to illustrate your point?
 
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];
 
((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;
}
 
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.
 
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.
 
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)
 
Back
Top