Pages

Subscribe:

Ads 468x60px

Saturday, October 13, 2012

how to clear all gridview elements in aps.net using c#

In this simple web Programming tutorial we will learn that how we can clear all Controls available in a gridview by just one click instead clearing one by one. like as we clear a form's text fields or other form elements. just copy the below code in your .CS file. Function call.
        this.ClearControls(GridView1);
        GridView1.RenderControl(htw);
Method to clear Gridview fields.
 private void ClearControls(Control ctrl)
    {
        for (int i = ctrl.Controls.Count - 1; i >= 0; i--)
        {
            ClearControls(ctrl.Controls[i]);
        }

        if (!(ctrl is TableCell))
        {
            if (ctrl.GetType().GetProperty("SelectedItem") != null)
            {
                LiteralControl literal = new LiteralControl();
                ctrl.Parent.Controls.Add(literal);
                try
                {
                    literal.Text = (string)ctrl.GetType().GetProperty("SelectedItem").GetValue(ctrl, null);
                }
                catch
                {
                }
                ctrl.Parent.Controls.Remove(ctrl);
            }
            else
                if (ctrl.GetType().GetProperty("Text") != null)
                {
                    if (ctrl.Visible == true)
                    {
                        LiteralControl literal = new LiteralControl();
                        ctrl.Parent.Controls.Add(literal);
                        literal.Text = (string)ctrl.GetType().GetProperty("Text").GetValue(ctrl, null);
                        ctrl.Parent.Controls.Remove(ctrl);
                    }
                }
            }
        return;
    }


No comments:

Post a Comment