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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Avoid having to use find control every time

Status
Not open for further replies.

jgd1234567

Programmer
May 2, 2007
68
GB
Hi i have a nested control i need to call in about 6 different methods. Therefore at the top of every method i have:

ListBox lbxLeftColumnAdverts = (ListBox)fvwDocument.FindControl("lbxLeftColumnAdverts");

Is it not possible to add a private variable to access this control. I tried placing the following at the top of my class:

private ListBox lbxLeftColumnAdverts = (ListBox)fvwDocument.FindControl("lbxLeftColumnAdverts");

I also tried setting it in the Page_Init method but had no joy. I'm sure this is quite a common problem but i seem to be missing something. Appreciate the help.

Thanks
 
what type of ojbect is fvwDocument? What events are accessing fvwDocument?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi fvwDocument is a FormView control. I have a series of ListBox controls within the formview with buttons to re arrange items in the boxes and pass items between the various boxes.
 
Is it not possible to add a private variable to access this control.
You don't have to assign it to a variable. You could just access it directly with the FindControl method.


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

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]
 
you can't do this directly FormView.FindControl("...") if your not looking for a container. (at least i don't think you can.)

instead you need to access the FormView's container and then call FindControl();

FormView.ItemTemplate.FindControl("...");

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi, sorry i think you might be mis-undertood me. I simply wanted a way of not having to declare:

ListBox lbxLeftColumnAdverts = (ListBox)fvwDocument.FindControl("lbxLeftColumnAdverts");

at the top of every method which uses the lbxLeftColumnAdverts control (which is within the formview control) as i feel it's abit long winded. I hoped i could just set a class level variable instead. If there's no solution then i guess i'll have to put up with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top