how to loop all controls of an web applications [Resolved]

Posted by Udaysimha under ASP.NET on 12/3/2011 | Points: 10 | Views : 1253 | Status : [Member] | Replies : 5
Hi,

foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox )
{
TextBox tb = (TextBox)x;
tb.Text = "";
}
}
like above is it any way is there for web application to loop all controls. Any help would be appreciated.

Thanks
Uday .

uday


Responses

Posted by: Varung on: 12/5/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
foreach(Control ctrl in ContentPlaceHolder.Controls)
{
if(ctrl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
TextBox id =(TextBox)ctrl;
id.Text = string.Empty;

}
if (ctrl.GetType().ToString().Equals("System.Web.UI.WebControls.DropDownList"))
{
DropDownList id = (DropDownList)ctrl;
id.SelectedIndex =0;

}
}

G.Varun

Udaysimha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Varung on: 12/3/2011 [Member] Starter | Points: 25

Up
0
Down
foreach(Control ctrl in Form.Control)
{
Type type=ctrl.GetType();
string id=ctrl.Id;
}
this will work dude....
if there are any issues reply.....







G.Varun

Udaysimha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Arefin on: 12/4/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Just try the following code:

foreach (Control cnt in this.Page.Controls)

{
if (cnt is TextBox)
{

}
}


You can also use with cnt.ID (as ID is all control common property to identify each other)

Good Luck !

Thanks,
Arefin

Udaysimha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ravindra.patil on: 12/5/2011 [Member] Starter | Points: 25

Up
0
Down
hi Varung ,

I tried your code it will work fine


thank you

Udaysimha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Udaysimha on: 12/5/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Varung,

Thanks for your reply. I traid your code it is working fine, but it looping the MasterPage controls only not content page controls. My actual requirement is finding textboxes then clear the text in textbox and finding dropdownlist then assing index value as 0 in a content page which is attached to a master page.


Thanks
Uday

uday

Udaysimha, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response