ASP.Net C# - Class Advice

T

TisMe

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";
}

}
 
K

Kevin Spencer

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
 
T

TisMe

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Accessing Control from Class 1
Class Setup Wrong...Help! 2
Simple question 4
shell command 1
Postback Question 2
Sharing a variable between all pages 5
Partial class problem 4
Class casting. 9

Top