Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I want to clear all text box of webpage using vb code and if i make it as function then what parameter should i pass in sub or function
Posted 21-Jan-13 3:47am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You could use a for each statment block
 
	For each txt as control in me.controls
	 if TypeOf txt is Textbox Then
	   txt.Text = string.empty
	  End if
	Next
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

public sub ClearTextboxes(controls as ControlCollection)
   for each ctrl as Control in controls 
      if TypeOf ctrl is Textbox Then
         ctrl.Text = String.Empty
      else if ctrl.HasControls
         ClearTextboxes(ctrl.Controls)
      end if
   next 
end sub
 
Then just call it from your code like this
   ....
   ClearTextboxes(me.Controls)
 
This method uses recursion to make sure all textboxes are cleared even if they are inside a container control.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you are clearing all textbox instances in your page, then you don't need any arguments in the function call. You just need to create a sub Private Sub ClearAllTextboxes () and then loop through all the controls in your page. For every control in your page, check if it is a textbox. If it is you can proceed to set the Text to string.Empty and voila, you have cleared all the textboxes.
  Permalink  
Comments
Jigar Sangoi at 21-Jan-13 11:27am
   
can you give me code because i tried but i am getting error
Marcus Kramer at 21-Jan-13 13:36pm
   
I pretty much have in my answer already. If you don't know how to deal with the simplest of Sub declarations and the simplest of loops within that then you probably need to study your course notes a little more carefully first. I'm not going to give you a complete code sample because if I do your homework for you, you learn nothing.
Adam R Harris at 21-Jan-13 15:17pm
   
I did end up just giving him a code sample simply because i can understand that the recursion required to check inside containers can be a little intimidating. I do however agree that if the basics of subs and loops is too much then one needs to review notes or take a course a second time.
 
Just wanted to let you know that my intention is not to undermine you but to assist with the more daunting task of recursion.
Marcus Kramer at 21-Jan-13 16:21pm
   
I would have preferred to have the OP post his code with the error so that we can see him/her actually making an effort to help themselves. I think it's just too easy for people to say "I have an error" in cases like this instead of actually trying. Cheers.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

you need to typecast textbox then it works in vb.net
instead of writing
ctrl.Text = String.Empty
you need to write
CType(ctrl, TextBox).Text = String.Empty
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100