4

我有一个按钮来显示有关客户的记录或信息,我刚刚注意到的问题是,当我选择一个提供了所有必要信息的客户时,该特定客户的信息会正确显示,现在当我尝试显示另一个客户时有一些缺失字段的信息,那个时候这个特定客户的缺失字段被以前的客户信息替换,这意味着我需要在显示客户信息之前清除文本框。这是我必须显示的方法信息。

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()));
    }
}

任何人都可以建议我如何清除文本框,我不想单独清除它们。感谢您的任何建议。

4

9 回答 9

11
foreach (var item in Page.Controls)
{
    if (item is TextBox)
    {
        ((TextBox)item).Text = "";
    }
}
于 2012-06-04T11:59:39.517 回答
2

试试这样。

foreach (var obj in Page.Controls.OfType<TextBox>())
{
   obj.Text="";
}
于 2012-06-04T12:03:22.650 回答
2

试试这个:

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 = ""; 
于 2012-06-04T12:03:23.190 回答
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);
        }
    }
}

其他答案确实是正确的,但是此代码还将设置作为孩子的孩子的文本框。基本递归将在任何具有自己的子控件的子控件上运行相同的循环。这样,您可以在特定控件及其子控件(如手风琴或内容块)中设置所有文本框。

于 2015-10-05T22:54:53.697 回答
1

Levi 的解决方案几乎没有更新,在我的场景中,我只想清除名为 grdDetails 的网格控件中的特定 texbox。将 fw 4.0 与 wpf 一起使用

foreach (var item in grdDetails.Children)
{
    if (item is TextBox)
    {
        ((TextBox)item).Text = "";
    }
}
于 2015-01-12T15:10:23.287 回答
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 = "";
        }
    }
}
于 2012-06-04T12:04:03.757 回答
0
private void  function ClearSection()
{
    foreach (Control cntrl in Page.Controls)
    {
        if (cntrl is TextBox)
        {
            ((TextBox)cntrl).Text = "";
        }
    }
}
于 2013-03-02T08:38:33.600 回答
0

而不是使用这么多行代码。您可以通过将文本框值设置为 null 属性来解决它。

看这里

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/
于 2013-09-28T11:35:32.140 回答
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);
        }
    }
}
于 2015-07-21T07:49:30.540 回答