Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear value of all textbox on Asp.Net buttons click

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.

like image 529
Priyank Patel Avatar asked Dec 02 '22 22:12

Priyank Patel


2 Answers

foreach (var item in Page.Controls)
{
    if (item is TextBox)
    {
        ((TextBox)item).Text = "";
    }
}
like image 113
levi Avatar answered Dec 18 '22 19:12

levi


try like this.

foreach (var obj in Page.Controls.OfType<TextBox>())
{
   obj.Text="";
}
like image 44
Ravi Gadag Avatar answered Dec 18 '22 17:12

Ravi Gadag