Tweaking overall user experience in C#.net project, a very effective trick you use to clear or reset all the controls placed on the web form or control. While building a form intensive site, page or control comprises number of text boxes, drop downs, check boxes, radio buttons etc. and user want to clear all the values. To accomplish this, we'll have a button and tie an event for clearing all fields. Pretty straight forward so far, below is the C# code :
private void ResetAllControls()
{
foreach (System.Web.UI.Control c in this.Controls)
{
switch (c.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox": ((TextBox)c).Text = string.Empty;
break;
case "System.Web.UI.WebControls.DropDownList": ((DropDownList)c).SelectedIndex = -1;
break;
case "System.Web.UI.WebControls.CheckBox": ((CheckBox)c).Checked=false;
break;
case "System.Web.UI.WebControls.CheckBoxList": ((CheckBoxList)c).ClearSelection();
break;
case "System.Web.UI.WebControls.RadioButton": ((RadioButton)c).Checked=false;
break;
case "System.Web.UI.WebControls.RadioButtonList": ((RadioButtonList)c).ClearSelection();
break;
}
}
}
Happy Coding....! :)
0 Comment(s)