4

I have a button to display the records or information about customers ,the problem I just noticed that when I select a customer who has provided all the necessary information , the information about that particular customer is displayed properly , now when I try to display another customers information who has some missing fields , that time the missing field of this particular customer is replaced by the previous customers infomration , that means I need a way to clear the textboxes before I display customers information.Here is the method that I have to display the information.

public void ShowCustomerInformationCat1()
{
    if (customer.cCustomerType != null)
    {
        ModifyCustomerByCategoryddlCustomerType.SelectedIndex = ModifyCustomerByCategoryddlCustomerType.Items.IndexOf(ModifyCustomerByCategoryddlCustomerType.Items.FindByText(customer.cCustomerType.Trim()));
        ModifyCustomerByCategoryddlNewCustomerType.SelectedIndex = ModifyCustomerByCategoryddlNewCustomerType.Items.IndexOf(ModifyCustomerByCategoryddlNewCustomerType.Items.FindByText(customer.cCustomerType.Trim()));
    }
    if (customer.cInitial != null)
    {
        ModifyCustomerByCategoryddlInitial.SelectedIndex = ModifyCustomerByCategoryddlInitial.Items.IndexOf(ModifyCustomerByCategoryddlInitial.Items.FindByText(customer.cInitial.Trim()));
        ModifyCustomerByCategoryddlNewInitial.SelectedIndex = ModifyCustomerByCategoryddlNewInitial.Items.IndexOf(ModifyCustomerByCategoryddlNewInitial.Items.FindByText(customer.cInitial.Trim()));
    }
    if (customer.cGender != null)
    {
        ModifyCustomerByCategoryddlGender.SelectedIndex = ModifyCustomerByCategoryddlGender.Items.IndexOf(ModifyCustomerByCategoryddlGender.Items.FindByText(customer.cGender.Trim()));
        ModifyCustomerByCategoryddlNewGender.SelectedIndex = ModifyCustomerByCategoryddlNewGender.Items.IndexOf(ModifyCustomerByCategoryddlNewGender.Items.FindByText(customer.cGender.Trim()));
    }
    if (customer.cCountry != null)
    {
        ModifyCustomerByCategoryddlCountry.SelectedIndex = ModifyCustomerByCategoryddlCountry.Items.IndexOf(ModifyCustomerByCategoryddlCountry.Items.FindByText(customer.cCountry.Trim()));
        ModifyCustomerByCategoryddlNewCountry.SelectedIndex = ModifyCustomerByCategoryddlNewCountry.Items.IndexOf(ModifyCustomerByCategoryddlNewCountry.Items.FindByText(customer.cCountry.Trim()));
    }
}

Can anybody suggest me how to clear the textboxes , I dont want to clear them individually. Thanks for any suggestions.

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Priyank Patel
  • 6,898
  • 11
  • 58
  • 88

9 Answers9

11
foreach (var item in Page.Controls)
{
    if (item is TextBox)
    {
        ((TextBox)item).Text = "";
    }
}
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
levi
  • 3,451
  • 6
  • 50
  • 86
2

try like this.

foreach (var obj in Page.Controls.OfType<TextBox>())
{
   obj.Text="";
}
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
2

Try this:

Control myForm = Page.FindControl("Form1.aspx"); 
foreach (Control ctl in myForm.Controls) 
if (ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")) 
((TextBox)ctl).Text = ""; 
coder
  • 13,002
  • 31
  • 112
  • 214
  • I am getting an error Object reference not set to an instance of an object.In place of Form1.aspx , I just replaced the name of my page. – Priyank Patel Jun 04 '12 at 12:08
2
private void SetTxtChildren(Control ParentControl,string NewValue = "", Boolean Enable = true)
{
    foreach (Control c in ParentControl.Controls)
    {
        if (c is TextBox)
        {
            ((TextBox)c).Enabled = Enable;
            ((TextBox)c).Text = NewValue;
        }
        else if (c.Controls.Count > 0)
        {
            ClearTxtChildren(c, Enable);
        }
    }
}

Other answer is indeed correct, but this code will also set textboxes that are children of children. Basic recursion will run the same loop on any child control that has child controls of its own. This way you can set all textboxes within a particular control and it's sub-controls (like accordions or content blocks).

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Thildemar
  • 99
  • 3
  • 12
1

little update for levi's solution, in my scenario I just wanted to clear specific texbox(s) which are in the grid control named grdDetails. using fw 4.0 with wpf

foreach (var item in grdDetails.Children)
{
    if (item is TextBox)
    {
        ((TextBox)item).Text = "";
    }
}
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
cmurat
  • 31
  • 7
0
protected void Button1_Click(object sender, EventArgs e)
{
    ClearSection();
    //Rest of your code
}

private void  function ClearSection()
{
    foreach (Control cntrl in Page.Controls)
    {
        if (cntrl is TextBox)
        {
            ((TextBox)cntrl).Text = "";
        }
    }
}
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Nikhil D
  • 2,479
  • 3
  • 21
  • 41
0
private void  function ClearSection()
{
    foreach (Control cntrl in Page.Controls)
    {
        if (cntrl is TextBox)
        {
            ((TextBox)cntrl).Text = "";
        }
    }
}
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
  • 3
    Code-only answers may solve the problem but they are much more useful if you explain how they solve it. – Nick Mar 28 '20 at 10:05
0

Instead of using So many lines of codes. You can solve it by just in a single code by just setting the text box value to null property.

see here

protected void btnSubmit_Click(object sender, EventArgs e)
{
    DataTable dt;
    USignUp USignUp = new USignUp();
    DateTime DOB;

    try
    {
        if (chkAccept.Checked == true)
        {
            DOB = Convert.ToDateTime(txtDOB.Text);
            int UserTypeId = 1;
            USignUp.SignUp_Insert(txtEmail.Text, txtPwd.Text, txtUFName.Text, txtULName.Text, txtCInfo.Text,
            DOB, UserTypeId);
            txtEmail.Text = "";
            txtPwd.Text = "";

            txtUFName.Text = "";
            txtULName.Text = "";
            txtCInfo.Text = "";
            txtCnfrEmail.Text = "";
            txtDOB.Text = "";
            txtCnfrmPwd.Text = "";
        }
        else
        {
            lblMessage.Visible = true;
            lblMessage.Text = "Please accept the terms and conditions";
            //Response.Write("<script LANGUAGE='JavaScript' >alert('Please select Checkbox');document.location='" + ResolveClientUrl("~/SignUp.aspx") + "';</script>");
        }
    }
    catch
    {
    }
}//It works.You can Know more on this by clicking to http://transinntech.com/
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
0
protected void Button1_Click(object sender, EventArgs e)
{
    refresh(this);
}

public void refresh(Control cont)
{
    foreach (Control x in cont.Controls)
    {
        if ((x.GetType() == typeof(TextBox)))
        {
            ((TextBox)(x)).Text = "";
        }

        if (x.HasControls())
        {
            CleartextBoxes(x);
        }
    }
}
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40