' For each control in this form For Each loControl In loHtmlForm.Controls ' If this is a textbox If loControl.GetType().ToString = "System.Web.UI.WebControls.TextBox" Then ' Assign this control to a textbox variable loTextbox = loControl ' Add the onkeypress to the textbox loTextbox.Attributes.Add("onkeypress", "FormKeyPress(event)") End If Next