Groups | Blog | Home
all groups > asp.net > may 2005 >

asp.net : Enable controls dynamically


Ed Chiu
5/13/2005 8:12:02 PM
Hi,

Is there a way to set webform controls to visible/invisible dynamically. Say
I have a textbox cann txtFirstName, I know the following statement can set it
visible:

txtFirstName.visible = true

Here is difficult part, I am tring to set the controls visible based on the
content of DataReader or DataSet. If the datareader has a row that has an
item "txtFirstName" then txtFirstName should be visible.

Sure I can use a big select statement to do this:

select case fieldName
case "txtFirstName":
txtFirstName.visible = true
'about another 50 case statements follow
end select

But is there a better way?

Teemu Keiski
5/14/2005 9:28:42 AM
Hi,

couldn't you run Page.FindControl based on what you have in data reader (do
it when looping the results). By default set Visible = False and when
looping the datareader, if control is found then set it to visible=true

While reader.Read()

Dim ctrl As Control=Page.FindControl(reader.GetString("ControlName"))
ctrl.Visible=True

End While
reader.Close()

If it needs to work in an environment where visibility might change between
postbacks, you probably need to have controls in some sort of array or list
where you would set others (not included in the datareader so set their
visibility to false), or then just loop through all controls on the Page to
hide those others.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU

[quoted text, click to view]

AddThis Social Bookmark Button