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

Clearing Page's Control

P: n/a
Ali

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?
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

"Ali" wrote:

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?

Nov 19 '05 #2

P: n/a
Ali
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

"Vishnu-Chivukula" wrote:
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.

"Ali" wrote:

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?

Nov 19 '05 #3

P: n/a
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();
}
}
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 19 '05 #4

P: n/a
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" <su***@comcreation-dot-com.no-spam.invalid> wrote in message
news:42********@127.0.0.1...
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();
}
}
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.