change an attribute of all controls, before the page renders

N

neilmcguigan

i want to enumerate all server controls in a page

if it has a text property, change it

right before the page is sent to the browser

how do i do this?

i take it i have to override something like page.Render()...

thanks!

neil
 
J

Jared

There may be an easier way, but if you chose a numeric naming
convention for your fields, than you can loop through them.

e.g.
str1, str2 //for text box
ddl1, ddl2 //for drop down list

CODE...

Page pgPage = this.Page; //or pass the page into the procedure if you
are using a class.

int intControl = 1;
int intControls = 55 //If you have 55 controls on your page

while(intControl <= 55)
{
TextBox strTextBox = pgPage.FindControl("str" + intControl) as TextBox;
strTextBox.Text = "Test"; //Set the value to whatever you want it to be
intControl += 1; //Move to the next control
}

//Repeat for DropDownLists and other control types, e.g.

while(intControl < 55)
{
DropDownList strDropDownList = pgPage.FindControl("ddl" + intControl)
as DropDownList;
strDropDownList.SelectedValue = "Test"; //Set the value to whatever you
want it to be
intControl += 1; //Move to the next control
}

In my application I have a whole load a variables for controls, so I
store them in a database table along with the control ID and perform
the same loop, but through a recordset.

Hope this is useful.

Jared
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top