473,941 Members | 7,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Enable ViewState = true;

}

Default.aspx
===========
<asp:PlaceHolde r ID="PlaceHolder 1" runat="server">

Default.aspx.cs
============
BaseClass sample = new BaseClass;
BaseClass.Creat eTextBox();

-------> 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 1516
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*******@gmai l.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.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.Enable ViewState = true;

}

Default.aspx
===========
<asp:PlaceHolde r ID="PlaceHolder 1" runat="server">

Default.aspx.cs
============
BaseClass sample = new BaseClass;
BaseClass.Creat eTextBox();

-------> 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.T ext = "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.Enable ViewState = true;

}

Default.aspx
===========
<asp:PlaceHolde r ID="PlaceHolder 1" runat="server">

Default.aspx.cs
============
BaseClass sample = new BaseClass;
BaseClass.Creat eTextBox();

-------> 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.Co ntrols.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.T ext = "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.Enable ViewState = true;

}

Default.aspx
===========
<asp:PlaceHolde r ID="PlaceHolder 1" runat="server">

Default.aspx.cs
============
BaseClass sample = new BaseClass;
BaseClass.Creat eTextBox();

-------> 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
2440
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 face. Say you have: class Car { virtual void speedup();
2
2794
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. The problem is the article is in VisualBasic. I already get the collection to be recognized as a Data Source by the IDE. It populated the DataGrid correctly from the fields on the items object of the collection, but I can't get the DataGrid to...
3
1612
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 app with; D m_D = new D(tkn); public class A : MarshalByRefObject public A () <--------------------- public class B : A public B () <----------------------
3
1282
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 successfully. 2. I add it to the user controls section of toolbox and it appears. 3. I add the new CyanFocusTextBoxes to the form and it all works. 4. The problem comes when I update the class CyanFocusTextBox in the dll and rebuild it. ...
2
1424
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 seems to work okay on its own, but when I try to bind it to a combobox, the combobox doesn't show anything (and it starts having problems repainting itself). The Datasource appears to contain my custom collection object, but when I ask for its item...
0
885
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 puts the code for the color as the default color in the class. But if I go back and change the color in the base class, all the buttons will not have the new color since the designer actually put the code for the color in each new class. I then...
15
2172
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 have an attribute which can be applied to a class definition of a class which inherits from a base, mustinherit class. I want to define methods in the base class which will access the contents of the attribute as it is applied to
5
4483
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 Another assembly, sharing the root namespace, contains a set of Custom attributes used to validate properties values, just like the Validation Application Block.
0
1060
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 helper methods that are at the list level, for instance, maybe we need to get some type of Average of MyClass's in the list so I have a method Average in the MyClassList class. One of the problems with this is if I use something like Find or FindAll...
0
10134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9860
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6080
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.