Specializations

Tuesday, November 13, 2012

Clear all control values using conditions in C#.net

 // we want to clear controls with in the panel or with in the group or with in the screen. we have to pass that contol if you want complete screen controls just pass this

Calling method:FindAllTextBox(this, false);


method implimentation:


public void FindAllTextBox(Control ctrl, bool flag)
        {

            if (ctrl != null)
            {
                foreach (Control c in ctrl.Controls)
                {
                    if (c is TextBox)
                    {
                        if (flag) ((TextBox)c).Text = string.Empty;
                        else
                            if ((c.ID != "txtCustomer") && (c.ID != "txtExporter") && (c.ID != "txtSONum") && (c.ID != "txtSODate"))
                                ((TextBox)c).Text = string.Empty;
                        if (c.ID == "txtSODate") ((TextBox)c).Text = DateTime.Now.ToString("MM/dd/yyyy");

                    }
                    if ((c is CheckBox) && (c.ID != "chkValidateParts"))
                        ((CheckBox)c).Checked = false;
                    if (c is DropDownList)
                        ((DropDownList)c).SelectedIndex = 0;

                    FindAllTextBox(c, flag);
                }
            }
        }


If you pass flag value is "true" controls are clear other wise exect condition control are not clear the values.


if you dont want any condition using bellow method

Method Calling:FindAllTextBox()


method implementations



public void FindAllTextBox()
        {

            if (ctrl != null)
            {
                foreach (Control c in this.Controls)
                {
                    if (c is TextBox)
                    {
                        if (flag) ((TextBox)c).Text = string.Empty;                       
                    }
                    if ((c is CheckBox) && (c.ID != "chkValidateParts"))
                        ((CheckBox)c).Checked = false;
                    if (c is DropDownList)
                        ((DropDownList)c).SelectedIndex = 0;
                    FindAllTextBox();
                }
            }
        }





No comments:

Post a Comment