Accessing Control from Class

T

TisMe

Hi All,

I have a problem that I am struck on, can you help?

1) I drag labelX onto the page
2) Create a new class 'myClass' which inherits system.web.ui.page
(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?

I have tried this (Within 'myClass')...

Label lblMess = new Label();
lblMess = (Label)FindControl("lblMessage");
lblMess.Text = "Hello Again World";

....which results in this error:
"Object reference not set to an instance of an object"

I know this means I am trying to access a null object, but really not
sure what to do to fix this?

I did think perhaps my method in 'myClass' needs to accept a byref
parameter type matching the control, is there another way? I'm sure I
am missing something simple here!

Thanks!!
Simon.
 
S

SMiGL

I have a problem that I am struck on, can you help?
1) I drag labelX onto the page
2) Create a new class 'myClass' which inherits system.web.ui.page
(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?

I have tried this (Within 'myClass')...

Label lblMess = new Label();
lblMess = (Label)FindControl("lblMessage");
lblMess.Text = "Hello Again World";

...which results in this error:
"Object reference not set to an instance of an object"

Perhaps 'myClass' not tie with you aspx page and so you can`t find
label.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="'<Name you
file>.aspx.cs"
Inherits="<Class name>" Title="Untitled Page" %>
 

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

Top