ledgilljele
New Member
Code ? Looping through controls in VS.Net<BR><BR><BR> Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Reset.Click<BR><BR> Dim tb As System.Web.UI.WebControls.TextBox<BR><BR> For Each tb In Page.Controls<BR> tb.Text = ""<BR> Next<BR><BR> End Sub<BR><BR><BR>Hi everybody I have a reset button on a form and I want to clear all the textboxes on the form when you hit reset obviously... I don't want to have to go <BR>textbox1.text = "" <BR>textbox2.text = "" <BR>textbox3.text = "" <BR>a zillion times but the code above keep giving me errors anybody have any ideas as to what I'm doing wrong here... <BR>Thanks for your help in advanceThis might work ... haven't tested it:<BR><BR>Dim tb As System.Web.UI.WebControls.TextBox<BR><BR>For Each tb In Page.Controls<BR> Dim somectrl as Control = FindControl(tb.id)<BR> If Not somectrl = Nothing and TypeOf somectrl Is TextBox Then <BR> CType(somectrl, TextBox).text = "" <BR> End If<BR>Next<BR><BR><BR>Or if you know the number of textboxes in your page and they all have the same name + a number:<BR><BR>Dim startNumber as integer = 0<BR>Dim endNumber as integer = 10<BR>Dim baseName as String = "myTextbox"<BR>Dim numerator as Integer<BR><BR>For numerator = startNumber to endNumber <BR> Dim somectrl as Control = FindControl(baseName & numerator)<BR> If Not somectrl = Nothing and TypeOf somectrl Is TextBox Then <BR> CType(somectrl, TextBox).text = "" <BR> End If<BR>Next<BR><BR><BR>Hope this helps <BR><BR>/JanDYou can also just make the set the type=reset property on the button and it will automatically clear all the fields for you.<BR><BR>This way you don't have to do any coding....<BR><BR>I've a reset button, but it never clears the text of any of my server controls. All these server controls have their viewstate set to true. So, when i click on reset button, it wouldn't clear any of them. Any suggestions, please do advice.The reset button resets the values of the html elements back to the value specified in the "value" attribute of the html tag. In a post back situation, the HTML page gets reloaded and the "value" attribute in the html tags of those input fields are set to their corresponding values in view state... So, I think the only way to get around this is to avoid any post back in the form. Otherwise, looks like you will have to set everything manually back to the original value, which is not that difficult if you simply loop through all the elements in the Controls collection.