I got this script while searching through stackoverflow
string commonTextBoxPrefix = "txt";
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(TextBox) &&
c.ID.StartsWith(commonTextBoxPrefix))
{
((TextBox)c).ReadOnly = true;
}
}
But when I loop through the controls its not detecting any other controls, it detects only {System.Web.UI.LiteralControl} So the loop no entering into
if (c.GetType() == typeof(TextBox)
Why the code not detecting Textboxes and other control.
I tried the below code also
foreach (Control c in parent.Controls)
{
if (c.Controls.Count > 0)
{
ClearControls(c);
}
else
{
switch (c.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
((TextBox)c).Text = "";
break;
case "System.Web.UI.WebControls.CheckBox":
((CheckBox)c).Checked = false;
break;
case "System.Web.UI.WebControls.RadioButton":
((RadioButton)c).Checked = false;
break;
case "System.Web.UI.WebControls.DropDownList":
((DropDownList)c).SelectedIndex = -1;
break;
}
}
}
But the loop not lets in to the conditions. Why the loop not detecting Textboxes? How can I set multiple textboxes to readonly.?
Works for me