views:

95

answers:

2

I'm trying to use FindControl() to SET the values of some DropDownLists on a page.

for some reason, it appears that FindControl() is returning a copy of the control object as opposed to a reference, I was under the impression that this would not be the case due to the lack of a copy constructor for Controls?

<EDIT>

Ok, it apparears that it is not in fact returning a copy, it's simply not letting me set the visibilty of a control, the other properties I'm setting work fine.

Does anyone have any insight as to why this might be the case? I've tried setting it in quick-watch mode then looking at the value straight away, and that isn't actually changing the value either!

</EDIT>

<EDIT> (two)

Ok, I'm doing this in Page_Load, and it's not in a gridview (I like how you guys assumed that one cus I was using FindControl()).

I'm doing this as there is a set of operations I have to perform on a dynamic number of similarly named lists, and it's MUCH better to do it in a loop than to hard code it.

</EDIT>

Can anyone help?

Cheers, Ed

+1  A: 

Where are you calling FindControl() and setting the Visible property?

For info, FindControl() gets a reference to a control in the current naming container, based on a string id and does not copy the control. In order to work with the control, you should cast it to the type of control that you expect -

DropDownList ddl = (DropDownList)e.Row.FindControl("myDropDownList");

this example would allow you to work with a DropDownList control in the OnRowDataBound event of a GridView.

Russ Cam
Already doing that, I'm setting other properties, as said above, some of these are DDL-specific.
Ed Woodcock
ok, where is your DropDownList - is it inside a GridView, inside an UpdatePanel? would you be able to post some of your aspx markup and code-behind?
Russ Cam
Nah, it's just straight on the page, I'm using findcontrol as there are multiple dropdowns with the names "CategoryIDropdown" where I is a number from one to a dynamic number based on information provided on page load. and I can't really post the aspx, not allowed by contract.
Ed Woodcock
ok, where are you setting the visibility? it could be a case that the visibility is set in one event handler method and then is overridden if/when Page_Load is runs again after that method
Russ Cam
it's set at the end of page_load, but the visibilty is reverted *instantly*, literally the line of code after.There's a method that renders some of the controls invisible to start with, but it's run right at the start of the Page_Load.
Ed Woodcock
do you have EnableViewState set on the control?
Russ Cam
+3  A: 

Edit:
Whoa, wait... are you setting the Visible property to true rather than false? In your comment, you mention it changes back instantly. The Visible property will evaluate to false if it's parent is set to be invisible, no matter how often you tell it the value should be true.

Thorarin
Sorry, the actual number of dropdowns is not dynamic, it's the number that are visible/editable that is, and this changes based on a large number of criteria. And the control is getting set to visible=false immediately after I call the visible=true, as if it was only a copy.
Ed Woodcock
Shit, totally forgot that they were all in invisible panels with their titles. . . .duh.Cheers!
Ed Woodcock
Good catch ! +1
Cerebrus
yeah, wp, stupid CategoryI and CategoryIDropdown =D
Ed Woodcock