Clearing Textboxes (Foreach) Not Working
-
Hi all. I am writing a program using Microsoft Visual Web Developer. I have 3 textboxes (TextBox1, TextBox2 and TextBox3) and a button. I want to use a foreach loop to clear textboxes. For some reason this code doesnt work on a Webform. While debugging it, the debugger skips the line: "if (c is TextBox)" and so the textboxes dont get cleared. Here control = {ASP.default_aspx} When I use the below code in Microsoft Visual C# with a windows form it works. What could be wrong? Here is the code: protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(this); } public void ClearTextBoxes(Control control) { foreach (Control c in control.Controls) { if (c is TextBox) { ((TextBox)c).Text = " "; } }
-
Hi all. I am writing a program using Microsoft Visual Web Developer. I have 3 textboxes (TextBox1, TextBox2 and TextBox3) and a button. I want to use a foreach loop to clear textboxes. For some reason this code doesnt work on a Webform. While debugging it, the debugger skips the line: "if (c is TextBox)" and so the textboxes dont get cleared. Here control = {ASP.default_aspx} When I use the below code in Microsoft Visual C# with a windows form it works. What could be wrong? Here is the code: protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(this); } public void ClearTextBoxes(Control control) { foreach (Control c in control.Controls) { if (c is TextBox) { ((TextBox)c).Text = " "; } }
Are your TextBoxes immediate descendants of the page? It's unlikely; you've probably got several layers of controls between them. Try this:
public void ClearTextBoxes(Control control)
{
foreach (Control c in control.Controls)
{
var textBox = c as TextBox;
if (textBox != null)
{
textBox.Text = " ";
}
else if (c.HasControls)
{
ClearTextBoxes(c);
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Hi all. I am writing a program using Microsoft Visual Web Developer. I have 3 textboxes (TextBox1, TextBox2 and TextBox3) and a button. I want to use a foreach loop to clear textboxes. For some reason this code doesnt work on a Webform. While debugging it, the debugger skips the line: "if (c is TextBox)" and so the textboxes dont get cleared. Here control = {ASP.default_aspx} When I use the below code in Microsoft Visual C# with a windows form it works. What could be wrong? Here is the code: protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(this); } public void ClearTextBoxes(Control control) { foreach (Control c in control.Controls) { if (c is TextBox) { ((TextBox)c).Text = " "; } }
try this code protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(Page.Controls); } void ClearTextBoxes(ControlCollection control) { foreach (Control ctrl in control) { if (ctrl is TextBox) ((TextBox)ctrl).Text = string.Empty; ClearInputs(ctrl.Controls); } }
-
Are your TextBoxes immediate descendants of the page? It's unlikely; you've probably got several layers of controls between them. Try this:
public void ClearTextBoxes(Control control)
{
foreach (Control c in control.Controls)
{
var textBox = c as TextBox;
if (textBox != null)
{
textBox.Text = " ";
}
else if (c.HasControls)
{
ClearTextBoxes(c);
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks!
-
try this code protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(Page.Controls); } void ClearTextBoxes(ControlCollection control) { foreach (Control ctrl in control) { if (ctrl is TextBox) ((TextBox)ctrl).Text = string.Empty; ClearInputs(ctrl.Controls); } }
Thanks man
-
Thanks man
your welcome
-
Hi all. I am writing a program using Microsoft Visual Web Developer. I have 3 textboxes (TextBox1, TextBox2 and TextBox3) and a button. I want to use a foreach loop to clear textboxes. For some reason this code doesnt work on a Webform. While debugging it, the debugger skips the line: "if (c is TextBox)" and so the textboxes dont get cleared. Here control = {ASP.default_aspx} When I use the below code in Microsoft Visual C# with a windows form it works. What could be wrong? Here is the code: protected void Button1_Click(object sender, EventArgs e) { ClearTextBoxes(this); } public void ClearTextBoxes(Control control) { foreach (Control c in control.Controls) { if (c is TextBox) { ((TextBox)c).Text = " "; } }
#region Clear all text boxes page value (for server-side controls) /// /// ex:FindTextBox(this); /// /// public void FindTextBox(Control c) { if (c.Controls != null) { foreach (Control x in c.Controls) { if (x is System.Web.UI.WebControls.TextBox) { ((System.Web.UI.WebControls.TextBox)x).Text = string.Empty; } FindTextBox(x); } } } #endregion #region Clear all text boxes page value (for client side controls) /// /// ex:FindHtmlInputText(this); /// /// public void FindHtmlInputText(Control c) { if (c.Controls != null) { foreach (Control x in c.Controls) { if (x is System.Web.UI.HtmlControls.HtmlInputText) { ((System.Web.UI.HtmlControls.HtmlInputText) x).Value = string.Empty; } if (x is System.Web.UI.HtmlControls.HtmlTextArea) { ((System.Web.UI.HtmlControls.HtmlTextArea)x).Value = string.Empty; } FindHtmlInputText(x); } } } #endregion ex:FindHtmlInputText(this); Hope you can help!!!