Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:18:06+00:00 2026-05-31T15:18:06+00:00

I got this script while searching through stackoverflow string commonTextBoxPrefix = txt; foreach (Control

  • 0

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.?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:18:07+00:00Added an answer on May 31, 2026 at 3:18 pm

    Works for me

    <asp:Panel ID="Panel1" runat="server">
                <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
                <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
                <asp:TextBox ID="txt3" runat="server"></asp:TextBox>
    </asp:Panel>
    
     protected void Page_Load(object sender, EventArgs e)
    {
        ClearTextBoxes(Panel1);
    }
    protected void ClearTextBoxes(Control control)
    {
        foreach (Control c in control.Controls)
        {
            if (c is TextBox && c.ID.StartsWith("txt"))
                ((TextBox)c).ReadOnly = true;
        }
    }
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a bash script that reads input from a file like this: while
I've got this script on my website : $(document).ready(function() { function filterPath(string) { return
I got this error , while running selenium script for Internet Explorer 9. Exception
I got this script called rapidshare link checker v2 php by Bigfish At the
I really need some quick tips here. I've got this VBScript script which sends
I've got this simple Perl script: #! /usr/bin/perl -w use strict; use Data::Dumper; my
I got this javascript code from off the internet. It's a script that changes
I just got help in how to compile this script a few mintues ago
I've got this bit of JQUERY: <script> function initNav() { $('.nav tr table td
I've got a Perl script that needs to execute another Perl script. This second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.