Clearing Page's Control

G

Guest

I used to clear my page's control in Visual Studio 2003 using code like this:
Dim c As Control
For Each c In Page.Controls(1).Controls
If TypeOf c Is TextBox Then
CType(c, TextBox).Text = Nothing
End If
If TypeOf c Is DropDownList Then
CType(c, DropDownList).SelectedIndex = 0
End If
Next

It worked properly. Now I am using Visual Studio 2005 beta 1 version and I
am using Master Page for my forms. I used the same code but nothing happens
... clearing controls did not work ... seems they change the page's control
hierarchy when using master pages but I don't exactly know. Does anyone know
how can I fix the above code to make it work in my VS 2005 application?
 
G

Guest

Hi Ali,

I am working in Whidby (VS.NET 2005) right now. The problem in the code is
that you need to take into account the master page form ID.

Run the form in the browser and then right click and view source. Now have a
look at the ID of the control(s). They will have a prefix of "ct100$..." or
something (this is what i had). This is a static value which will be appended
to all your controls in the page. Modify your code and append this value. It
will work fine.

Need any help, do post a msg back...


Happy Coding.
 
G

Guest

Hi Vishnu,
Yes, you're right. I checked my code and found this:
<input name="ctl00$ContentColumn$Address" type="text"
id="ctl00_ContentColumn_Address" style="height:22px;width:183px;" />
<input name="ctl00$ContentColumn$city" type="text"
id="ctl00_ContentColumn_city" style="height:22px;width:183px;" />

Those two textboxes have IDs: address and city .. and as you said the
clt00$_ContentColumn_ appended to all controls' IDs on the page. Now i want
to know how to append this value to my code below .. what changes should I
make:

For Each c In Page.Controls(1).Controls
If TypeOf c Is TextBox Then
CType(c, TextBox).Text = Nothing
End If
Next
Thanx
 
P

pvsunil

hi,

can anybody help me out the code in asp.net

i have used the following code. but it is not getting the output. Can
anybody check and tell me whether the following code is correct.

regards,

Sunil
------------------------------------------------------------------------

foreach (System.Web.UI.Control oChildControl in Page.Controls)
{
if
(oChildControl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
bool TypeFlag = oChildControl.GetType().ToString();
}
}
 
K

Kevin Spencer

You want to loop through the Controls Collection of the form, not the Page.
The reason is that the Controls Collection of a Control is only 1 deep. That
is, if you have Controls nested inside other Controls, the Controls
Collection of any Control in the hierarchy is only the Controls immediately
below that Control, not any Controls inside those Controls.

The WebForm itself is a Control inside the Page Control.So, if you're
looking for a Control inside the Web Form, you look in its Controls
Collection.

It is also possible to create a recursive function that loops through the
Controls of a given Control, and then calls itself for each Control inside
that Control recursively.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

pvsunil said:
hi,

can anybody help me out the code in asp.net

i have used the following code. but it is not getting the output. Can
anybody check and tell me whether the following code is correct.

regards,

Sunil
------------------------------------------------------------------------

foreach (System.Web.UI.Control oChildControl in Page.Controls)
{
if
(oChildControl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
bool TypeFlag = oChildControl.GetType().ToString();
}
}
 

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

Top