473,394 Members | 2,052 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,394 software developers and data experts.

ASP.Net C# - Class Advice

Hi All,

Can someone please advise how I make controls on the page accessible
to class myClass, as defined below?

Any help will be appreciated!

Thank you.

Simon.
public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
// Do stuff with controls - No problem...
lblMessage.Text = "Hello World";
}

}

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

public myClass
{
}

public void setLabel
{
// This doesn't work...(Doesn't exist in the current
context)
lblMessage.Text = "Hello Again World!";

// Neither does this...(Null reference exception)
Label lblMess = new Label();
lblMess = (Label)FindControl("lblMessage");
lblMess.Text = "Hello Again World";
}

}
Dec 6 '07 #1
2 1081
They have to be members of the class. In your first example, the class
definition is partial, which means that the label is in another part of the
class definition. Apparently, there is no label member in your second
(non-partial) implementation.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"TisMe" <si**********@googlemail.comwrote in message
news:72**********************************@n20g2000 hsh.googlegroups.com...
Hi All,

Can someone please advise how I make controls on the page accessible
to class myClass, as defined below?

Any help will be appreciated!

Thank you.

Simon.
public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
// Do stuff with controls - No problem...
lblMessage.Text = "Hello World";
}

}

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

public myClass
{
}

public void setLabel
{
// This doesn't work...(Doesn't exist in the current
context)
lblMessage.Text = "Hello Again World!";

// Neither does this...(Null reference exception)
Label lblMess = new Label();
lblMess = (Label)FindControl("lblMessage");
lblMess.Text = "Hello Again World";
}

}

Dec 7 '07 #2
On Dec 7, 12:20 pm, "Kevin Spencer" <unclechut...@nothinks.comwrote:
They have to be members of the class. In your first example, the class
definition is partial, which means that the label is in another part of the
class definition. Apparently, there is no label member in your second
(non-partial) implementation.
Hi, thanks for this - So where have I gone wrong? (e.g. What should I
have done?)

Simpler example:

1) I drag labelX onto the page
2) Code myClass (Outside of the _default class for the page)
3) I now want to change a property of labelX, from a method within
myClass - How can I do this?

Thank you.
Dec 7 '07 #3

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

Similar topics

3
by: fernandez.dan | last post by:
I'm still learning how to use Object Oriented concepts. I'm have a basic question dealing with design. I have two classes that deal with I/O pertaining to network and usb that inherit from an...
4
by: Peter Speybrouck | last post by:
I have a little problem with a webservice. I reproduced the problem in the following simplified example. I just create a new C# ASP.NET webservice and a c# console application. I added a new...
5
by: Jeff Cobelli | last post by:
I am trying to include two classes as members of another class in a webservice. The definitions look basically like this: Public Class clsLender Public ID As String Public Name As String End...
6
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
2
by: Fish | last post by:
I have been researching the correct way to organize my solution so that it makes best use of VB.NET inherent ability to manage resources such as objects. My solution contains 2 projects and the...
5
by: henkoo | last post by:
i want to explicit instantiate a member function of a class template, and got some error, can anyone give some advice to correct it? 3x complier: g++ 3.2 #include <iostream> #include...
9
by: Kobe | last post by:
Is there any difference in: template <class T> vs. template <typename T> ?
2
by: mgoold2002 | last post by:
Hello. I've just begun programming in VB .NET, and I'm trying to turn all my modules into classes. In order to retrieve/exchange values from one class to another, I initiated New instances of the...
14
by: junw2000 | last post by:
Is it possible to create an abstract base class without a pure virtual function? Thanks Jack
10
by: HCB | last post by:
Hello: The book "Code Complete" recommends that you put only one class in a source file, which seems a bit extreme for me. It seems that many classes are small, so that putting several of them...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...
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...

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.