FindControl with a string variable as the parameter

  • Thread starter Mark D'Souza via .NET 247
  • Start date
M

Mark D'Souza via .NET 247

In trying to create a Panel object with the FindControl methodusing a string variable as the paramater. The code is below. The variable 'testvariable' is equal to pnl_0 which is anexisting Panel on my page. It appears that I am unable to use avariable as the parameter for the method as I get the followingerror message:

"Object reference not set to an instance of an object"

CODE:
Panel pnl = (Panel) FindControl(testvariable);
pnl.Visible = false;

Any assistance would be greatly appreciated. I'm been playingaround with it for the last litte while and cannot find a workaround.
 
D

DalePres

First, it is a good idea to test for null on the results of FindControl in
case the object is not found.

I tried your code with no problems. I created the variable testvariable and
assigned the string "pnl_0" to it. I successfully found the panel with
FindControl. I also set testvariable = pnl_0.ID and was able to return the
panel.

If you're not able to locate your panel with FindControl, you have either
mispelled your ID when assigning the value to testvariable, or your panel is
not at the Page level. Make sure it is not a child control of some other
container. If it is a child control, you'll have to do a recursive search
to get to your pnl_0.

HTH

DalePres
MCAD, MCDBA, MCSE



In trying to create a Panel object with the FindControl method using a
string variable as the paramater. The code is below. The variable
'testvariable' is equal to pnl_0 which is an existing Panel on my page. It
appears that I am unable to use a variable as the parameter for the method
as I get the following error message:

"Object reference not set to an instance of an object"

CODE:
Panel pnl = (Panel) FindControl(testvariable);
pnl.Visible = false;

Any assistance would be greatly appreciated. I'm been playing around with
it for the last litte while and cannot find a work around.
 
B

Bob Calvanese

Try setting testvariable equal to pnl_0.Name.

Bob Calvanese

In trying to create a Panel object with the FindControl method using a
string variable as the paramater. The code is below. The variable
'testvariable' is equal to pnl_0 which is an existing Panel on my page. It
appears that I am unable to use a variable as the parameter for the method
as I get the following error message:

"Object reference not set to an instance of an object"

CODE:
Panel pnl = (Panel) FindControl(testvariable);
pnl.Visible = false;

Any assistance would be greatly appreciated. I'm been playing around with
it for the last litte while and cannot find a work around.
 

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