Whats the easiest way to clear an asp.net form at runtime using c#.
Thanks Sp
From stackoverflow
-
I assume you want to clear input boxes, dropdowns etc. This can be done the following way in code to recursivly clear all data.
foreach( var control in this.Controls ) { ClearControl( control ); }and the recursive function
private void ClearControl( Control control ) { var textbox = control as TextBox; if (textbox != null) textbox.Text = string.Empty; var dropDownList = control as DropDownList; if (dropDownList != null) dropDownList.SelectedIndex = 0; // handle any other control // foreach( Control childControl in control.Controls ) { ClearControl( childControl ); } }Steven : Thanks for this, i was unable to get it working as im using HTML controls, I switched the textbox to htmlinputtext but still failed. I have used some js to clear the form Thanks for your post Sp -
I have used the following JS/c# to clear the form.
c# to add the js call onload
Page.ClientScript.RegisterStartupScript(typeof(WebForm3), "ClearPage", "ClearForm();", true);the JS to clear the form
function ClearForm() { var AllControls = document.getElementById('ctl00_ContentPlaceHolder1_PnlAll') var Inputs = AllControls.getElementsByTagName('input'); for (var y = 0; y < Inputs.length; y++) { // define element type type = Inputs[y].type // alert before erasing form element //alert('form='+x+' element='+y+' type='+type); // switch on element type switch (type) { case "text": case "textarea": case "password": //case "hidden": Inputs[y].value = ""; break; } } }
0 comments:
Post a Comment