我有一个按钮来显示有关客户的记录或信息,我刚刚注意到这个问题,当我选择提供所有必要信息的客户时,有关该特定客户的信息显示正确,现在当我尝试显示其他客户信息,其中包含一些缺少的字段,此时此特定客户的缺失字段被以前的客户信息所取代,这意味着我需要一种方法来清除文本框,然后才显示客户信息。这是我的方法显示信息。
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()));
}
}
有人可以建议我如何清除文本框,我不想单独清除它们。 感谢您的任何建议。
答案 0 :(得分:11)
foreach (var item in Page.Controls)
{
if (item is TextBox)
{
((TextBox)item).Text = "";
}
}
答案 1 :(得分:2)
试试这样。
foreach (var obj in Page.Controls.OfType<TextBox>())
{
obj.Text="";
}
答案 2 :(得分: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 = "";
答案 3 :(得分: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);
}
}
}
其他答案确实是正确的,但此代码还将设置作为子项的子项的文本框。基本递归将在具有自己的子控件的任何子控件上运行相同的循环。这样,您可以在特定控件及其子控件(如手风琴或内容块)中设置所有文本框。
答案 4 :(得分:1)
foreach (var item in grdDetails.Children)
{
if (item is TextBox)
{
((TextBox)item).Text = "";
}
}
答案 5 :(得分: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 = "";
}
}
}
答案 6 :(得分:0)
private void function ClearSection()
{
foreach (Control cntrl in Page.Controls)
{
if (cntrl is TextBox)
{
((TextBox)cntrl).Text = "";
}
}
}
答案 7 :(得分: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/
答案 8 :(得分: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);
}
}
}