By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,455 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,455 IT Pros & Developers. It's quick & easy.

any way to do this?

P: n/a
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;
Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Page.FindControl("mycontrolname")

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx

"Robert Megee" <rm*****@comcast.net> wrote in message
news:u0********************************@4ax.com...
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

Nov 16 '05 #2

P: n/a
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" <rm*****@comcast.net> wrote in message
news:u0********************************@4ax.com...
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

Nov 16 '05 #3

P: n/a
"Robert Megee" <rm*****@comcast.net> wrote in message
news:u0********************************@4ax.com...
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;
}
Nov 16 '05 #4

P: n/a
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
On Fri, 11 Mar 2005 09:21:54 +0700, "Alexander Shirshov"
<al*******@omnitalented.com> wrote:
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" <rm*****@comcast.net> wrote in message
news:u0********************************@4ax.com.. .
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


Nov 16 '05 #5

P: n/a
I'll check it out. Thanks.

Robert
On Thu, 10 Mar 2005 21:18:17 -0500, "Robbe Morris [C# MVP]"
<in**@turnkeytools.com> wrote:
Page.FindControl("mycontrolname")


Nov 16 '05 #6

P: n/a
string str1;
str1 = "textbox1";
Control myControl1 = FindControl(str1);
if(myControl1!=null)
{
myControl1.Visible = true;
}

Sweet! This will be perfect! many thanks.

Robert
Nov 16 '05 #7

P: n/a
Ask for a WebForms solution and receive WinForms one for free!
"Robert Megee" <rm*****@comcast.net> wrote in message
news:19********************************@4ax.com...
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
On Fri, 11 Mar 2005 09:21:54 +0700, "Alexander Shirshov"
<al*******@omnitalented.com> wrote:
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" <rm*****@comcast.net> wrote in message
news:u0********************************@4ax.com. ..
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

Nov 16 '05 #8

P: n/a
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
Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.