Groups | Blog | Home
all groups > asp.net building controls > september 2004 >

asp.net building controls : controls getting values from another control---within a control


DC Gringo
9/15/2004 12:22:10 PM
I am a bit twisted and need some straightening out. I have a webform with 3
controls on it: a mainHeader, a sectionHeader with a label control contained
in the mainHeader, and a topHeadline.

In the topHeadline, I am retrieving a value with a stored procedure. I need
to set that value to a label control within the sectionHeader like this:

mainHeader1.sectionHeader1.sectionHeaderLabel.Text = myVariable

In topHeadlines, I have:

Public countryName As String
Protected mainHeader1 As mainHeader
Protected sectionHeader1 As sectionHeader

(In Page Load): mainHeader1.sectionHeader1.sectionHeaderLabel.Text =
myVariable

The error I get is: "Object reference not set to an instance of an object."

What am I doing wrong?

_____
DC G



--
_____
DC G

Martin Dechev
9/16/2004 9:59:15 AM
Hi, DC Gringo,

My preferred approach is to create an interface and then implement it in all
page classes that have the mentioned controls in them. In your case the
interface will have one method in it:

Public Interface IPageWithHeaders
Public Sub SetLabelText(ByVal text As String)
End Interface

Then in the implementation in each page class you will access the control in
sectionHeader (the recommended way is to expose the sectionHeaderLabel.Text
as public string property of the sectionHeader class) and set its value to
the value of the passed parameter.

In topHeadline you will cast the Page property to your interface and call
the method:

DirectCast(Me.Page, IPageWithHeaders).SetLabelText(someValue)

Hope this helps
Martin
[quoted text, click to view]

AddThis Social Bookmark Button