Enable controls dynamically

G

Guest

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?

TIA
 
T

Teemu Keiski

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.
 

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

Members online

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,367
Latest member
Monarch

Latest Threads

Top