473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Create custom base class to reuse code throughout site -- can't seem to get it to work!

Hey,

I'm trying to create a simple class to create a textbox, and then
instantiate it from a page (so I can resue it across an asp.net app).
I've been scratching my head but I can't get this to work. I am I in
the right direction?

BaseClass.cs
============
public class BaseClass
{
protected TextBox textBox1;
public BaseClass()
{
}
public void CreateTextBox()
{
TextBox textBox1 = new TextBox();
textBox1.Text = "Hello from BaseClass";
textBox1.EnableViewState = true;

}

Default.aspx
===========
<asp:PlaceHolder ID="PlaceHolder1" runat="server">

Default.aspx.cs
============
BaseClass sample = new BaseClass;
BaseClass.CreateTextBox();

-------> It all compiles but no textbox appears.

1) How do I "tell" the BaseClass.cs to look for the PlaceHolder1 on the
default.aspx page? I always get a not in the current context problem.

2) And how do I "tell" default.aspx.cs that the textBox1 is the
textbox to use?

Any ideas?

Thanks!
-David

May 25 '06 #1
3 1484
If it's actually a base class, your code should look like:

public clas BaseClass : Page
{
blah
}

//default.aspx.cs
public class _Default : BaseClass
{
//blah
}
if all pages that inherit from BaseClass are going to have a placeholder,
you can declare it in your base class, something like:

protected PlaceHolder PlaceHolder1;

Karl

--
http://www.openmymind.net/

"Ranginald" <da*******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hey,

I'm trying to create a simple class to create a textbox, and then
instantiate it from a page (so I can resue it across an asp.net app).
I've been scratching my head but I can't get this to work. I am I in
the right direction?

BaseClass.cs
============
public class BaseClass
{
protected TextBox textBox1;
public BaseClass()
{
}
public void CreateTextBox()
{
TextBox textBox1 = new TextBox();
textBox1.Text = "Hello from BaseClass";
textBox1.EnableViewState = true;

}

Default.aspx
===========
<asp:PlaceHolder ID="PlaceHolder1" runat="server">

Default.aspx.cs
============
BaseClass sample = new BaseClass;
BaseClass.CreateTextBox();

-------> It all compiles but no textbox appears.

1) How do I "tell" the BaseClass.cs to look for the PlaceHolder1 on the
default.aspx page? I always get a not in the current context problem.

2) And how do I "tell" default.aspx.cs that the textBox1 is the
textbox to use?

Any ideas?

Thanks!
-David

May 25 '06 #2
In default.aspx.cs you don't ever set the text box to be displayed
anywhere. Where do you want to place it?

You can expose the textbox1 as a property from BaseClass to allow
default.aspx and other classes to access it.

E.g.

public property TextBox MyTextBox
{
get ( return textBox1; }
set { textBox1 = value; }
}

Then in default.aspx.cs you can access it:

BaseClass _bc = new BaseClass();

_bc.MyTextBox.Text = "Hello World";

Then you can use this property to place it on the form somewhere.

Ranginald wrote:
Hey,

I'm trying to create a simple class to create a textbox, and then
instantiate it from a page (so I can resue it across an asp.net app).
I've been scratching my head but I can't get this to work. I am I in
the right direction?

BaseClass.cs
============
public class BaseClass
{
protected TextBox textBox1;
public BaseClass()
{
}
public void CreateTextBox()
{
TextBox textBox1 = new TextBox();
textBox1.Text = "Hello from BaseClass";
textBox1.EnableViewState = true;

}

Default.aspx
===========
<asp:PlaceHolder ID="PlaceHolder1" runat="server">

Default.aspx.cs
============
BaseClass sample = new BaseClass;
BaseClass.CreateTextBox();

-------> It all compiles but no textbox appears.

1) How do I "tell" the BaseClass.cs to look for the PlaceHolder1 on the
default.aspx page? I always get a not in the current context problem.

2) And how do I "tell" default.aspx.cs that the textBox1 is the
textbox to use?

Any ideas?

Thanks!
-David

May 25 '06 #3
You can then add the control to the placeholder:

PlaceHolder1.Controls.Add(_bc.MyTextBox);

Ray Booysen wrote:

In default.aspx.cs you don't ever set the text box to be displayed
anywhere. Where do you want to place it?

You can expose the textbox1 as a property from BaseClass to allow
default.aspx and other classes to access it.

E.g.

public property TextBox MyTextBox
{
get ( return textBox1; }
set { textBox1 = value; }
}

Then in default.aspx.cs you can access it:

BaseClass _bc = new BaseClass();

_bc.MyTextBox.Text = "Hello World";

Then you can use this property to place it on the form somewhere.

Ranginald wrote:
Hey,

I'm trying to create a simple class to create a textbox, and then
instantiate it from a page (so I can resue it across an asp.net app).
I've been scratching my head but I can't get this to work. I am I in
the right direction?

BaseClass.cs
============
public class BaseClass
{
protected TextBox textBox1;
public BaseClass()
{
}
public void CreateTextBox()
{
TextBox textBox1 = new TextBox();
textBox1.Text = "Hello from BaseClass";
textBox1.EnableViewState = true;

}

Default.aspx
===========
<asp:PlaceHolder ID="PlaceHolder1" runat="server">

Default.aspx.cs
============
BaseClass sample = new BaseClass;
BaseClass.CreateTextBox();

-------> It all compiles but no textbox appears.

1) How do I "tell" the BaseClass.cs to look for the PlaceHolder1 on the
default.aspx page? I always get a not in the current context problem.

2) And how do I "tell" default.aspx.cs that the textBox1 is the
textbox to use?

Any ideas?

Thanks!
-David

May 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew Ward | last post by:
Hi All, Sorry if this is off topic, but I could not seem to find a suitable OO Design newsgroup. If there is one feel free to let me know. Here is a simplification of a general design problem I...
2
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
3
by: Wayne | last post by:
1. I have created my own class that inherits the textbox (called it CyanFocusTextBox). I put in some code and some new properties. All this works. I build the dll that contains this class...
2
by: Don | last post by:
I've been trying to create a custom collection that can be bound to a combobox via the DataSource property, but I can't seem to get it to work. I've created a class that implements IList, and it...
0
by: code3_brent | last post by:
I use a certain look for a button throughout my application, so I created a class that inherits from Button and set the colors how I want. Now when I place that new button on a form, the designer...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
5
by: Sergio Montero | last post by:
I have a MustInherits Base class that implements a custom IDataLayer interfase. IDataLayer expose CRUD methods. Base class constructor requires two parameters: ConnectionString TableName ...
0
by: jappenzeller | last post by:
I've got a coding standard question for generic lists and .NET 2.0. I have created some custom strongly typed lists like, MyClassList:List<MyClass>. We did this because we wanted to reuse some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.