Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FindControl problem 1

Status
Not open for further replies.

Waynest

Programmer
Jun 22, 2000
321
GB
asp.net 2

Hi

Could anyone suggest why ths bit of code would cause 'Oject referenece not set to instance of an object' message?

There is most definitely a label with ID 'lblSlot1' on the page. The page is a content of a master page if thats significant, but not I'm trying to reference controls on one from the other or anything like that.


myString = "lblSlot1";
Label myLabel = ((Label)(Page.FindControl(myString)));
myLabel.Text = "test text";

Thanks!
 
The label's parent probably isn't the Page, so it doesn't find it directly. Either:

1) Specify the parent yourself (i.e. put it in a Panel and use the Panel's FindControl method)
2) Write a recursive function to look through all controls for the Label
3) Do some debugging through the Controls collection and find out exactly where the control is located


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Thanks, that helped. Here is required syntax for others with similar problem.


Control myControl = this.Master.FindControl("ContentPlaceHolder1").FindControl(myString);

if (myControl != null)
{
(myControl as Label).Text = "testtext";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top