variable label control

  • Thread starter Thread starter Ankit Aneja
  • Start date Start date
A

Ankit Aneja

I'm trying to use a for() loop to go through a set of labels and set their
visibility to false.

I had to do something like

Code:

for(int i=1;i<=10;i++)
{
labeli.visible = false;
}

or
for(int i=1;i<=10;i++)
{
String s;
s=("Label"+i);
Label lab=new Label();
lab.ID=s;
lab.Visible=false;

}

but i am not getting how to do that pls help
 
Hi,

Use the page object's controls collection
e.g

int i = 1;
foreach (Control ctl in Page.Controls)
{
if (ctl.name == "label" & i)
{
ctl.visible = false;
}
}

Well, this might not be the cleanest way. But this can be the way to go

HTH
Kalpesh
 
Kalpesh said:
Hi,

Use the page object's controls collection
e.g

int i = 1;
foreach (Control ctl in Page.Controls)
{
if (ctl.name == "label" & i)
{
ctl.visible = false;
}
}

Well, this might not be the cleanest way. But this can be the way to
go

HTH
Kalpesh

Two remarks:
1) the "&" operator is "bitwise AND" in C# (and string-concat in VB).
Use "+" to concatenate strings in C#
2) the Controls collection lists only one level deep. You need to call this
recursively to find "deeper" controls

Hans Kesting
 
Thanks Hans for correcting :)
I assumed that the label controls are those put by the user explicitly

Kalpesh
 
i am not able to get ctl.name
when i write ctl.
i doesn't show property named "name"
 
Use the ID property. Thats the name with which you identify a control
on server side execution

HTH
Kalpesh
 
Hi Ankit,

System.Web.UI.Control only has "ID" property to identify itself, not
"Name". Also, we need to search from the HtmlForm control to loop all the
labels you defined at the top page level( directly under the <form
runat=server>...</from> rather than nested in other container controls).
The following code is just a simple example to loop all the top level
labels:

====================
HtmlForm form1 = Page.FindControl("Form1") as HtmlForm;

foreach(Control ctrl in form1.Controls)
{
if(ctrl is Label)
{
Response.Write("<br>" + ctrl.ID);
}
}
====================

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "Ankit Aneja" <[email protected]>
| References: <[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
| Subject: Re: variable label control
| Date: Tue, 18 Oct 2005 15:04:54 +0530
| Lines: 10
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: dsl-del-dynamic-032.76.246.61.touchtelindia.net
61.246.76.32
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:132025
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| thanks
| | > Thanks Hans for correcting :)
| > I assumed that the label controls are those put by the user explicitly
| >
| > Kalpesh
| >
|
|
|
 

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

Back
Top