Tuesday, 12 April 2016

clear controls in asp.net

Suppose you have the list of input control that you want to clear it after saving the records or updating the records, you can use the generic method and that you can call from your derived class and pass the control.

Use below method  for same it will clear your input control from C# code behind.



// <summary>
        /// Clear input values in the supplied control collection
        /// </summary>
        /// <param name="controls">(ControlCollection) The collection of controls to clear</param>
        public static void ClearInputValues(ControlCollection controls)
        {
            foreach (System.Web.UI.Control ctl in controls)
            {
                if (ctl is WebControl)
                {
                    if (ctl is TextBox)
                    {
                        ((TextBox)ctl).Text = string.Empty;
                    }
                    else if (ctl is DropDownList)
                    {
                        ((DropDownList)ctl).SelectedIndex = 0;
                    }
                    else if (ctl is CheckBox)
                    {
                        ((CheckBox)ctl).Checked = false;
                    }
                    else if (ctl is ListBox)
                    {
                        ((ListBox)ctl).ClearSelection();
                    }
                }
            }
        }

No comments:

Post a Comment

Others

You will like to read it