any way to do this?

  • Thread starter Thread starter Robert Megee
  • Start date Start date
R

Robert Megee

I'd like to be able to make a web controls such as a textbox
either visible or not-visible by using the name of the textbox
in a string and using that string in some command.
sort of like:
string str1;
 
Robert,

It's not actually clear to me what you want, but I'll answer anyway :-)

This (untested!) function enumerates all controls in a container and if the
control name matches

void SetVisible (ControlCollection controls, string controlName, bool
isVisible)
{
foreach (Control control in controls)
{
if (String.Compare(control.Name, controlName, true)) == 0)
{
control.Visible = isVisible;
return;
}
}

From the form class you can use it like this:

SetVisible(this, "txtName", true);

You might want to call this function recursively if you use control
containers like Panel.

HTH,
Alexander
 
Robert Megee said:
I'd like to be able to make a web controls such as a textbox
either visible or not-visible by using the name of the textbox
in a string and using that string in some command.
sort of like:
string str1;
.
.
.
str1 = "textbox1";
.
.
.
@str1.Visible = true;

where the @ would mean substitue the value of the
string in this context.

I've seen a reference to pointers such as are used in C, but I can't
find any examples. Perhaps this is a way to do this.

Thanks,

Robert


string str1;
str1 = "textbox1";
Control myControl1 = FindControl(str1);
if(myControl1!=null)
{
myControl1.Visible = true;
}
 
Since there are several controls that I want to toggle, this
may prove quite useful. I'll see what I can do with it.
thanks!

Robert
 
string str1;
str1 = "textbox1";
Control myControl1 = FindControl(str1);
if(myControl1!=null)
{
myControl1.Visible = true;
}
Sweet! This will be perfect! many thanks.

Robert
 
Alexander

Combine the answers from Robbe and Robbert and than you have a webform
solution.

A sample modify it for your own needs.
\\\
Control frm = this.FindControl("Form1");
foreach (Control ctl in frm.Controls)
if (ctl is Button)
((Button) ctl).BackColor =
System.Drawing.Color.Azure;
///

I hope this helps,

Cor
 
Back
Top