-1

I have master page and its content page. I want to get the textboxes situated on Content page. How can I get them in C# code behind of Content page?

Brian
  • 11,132
  • 7
  • 32
  • 43
Red Swan
  • 13,873
  • 43
  • 146
  • 234
  • Get them in the masterpage or something else? – RichardOD Dec 21 '09 at 15:10
  • RichardOD: As I read the question it states "How can I access the TextBox on the Content page from code behind of the content page". This should be trivial enough but he might want to know how to access it from the masterpage but the question does not say that. – Sani Singh Huttunen Dec 21 '09 at 15:15

1 Answers1

0

Access it by using it's ID:

Markup:

<asp:TextBox runat="server" ID="myTextBox"></asp:TextBox>

Code behind:

myTextBox.Text = "This is a text!";

Note that you need to add runat="server" for it to be accessible.

Edit
After your comment I think I understand where you're getting at. You'll need to traverse through all controls down the control tree to find all TextBoxes.

Here is a recursive implementation:

public List<Control> FindControls(Control root, Type type)
{
    var controls = new List<Control>();

    foreach (Control ctrl in root.Controls)
    {
        if (ctrl.GetType() == type)
            controls.Add(ctrl);

        if (ctrl.Controls.Count > 0)
            controls.AddRange(FindControls(ctrl, type));
    }

    return controls;
}

To get all TextBoxes in a page you'll call it with Page as root Control:

var allTextBoxes = FindControls(Page, typeof(TextBox));

The above example is to clarify the idea of how you should proceed. I'd do it a bit differently by using an Extension Method:

public static class ExtensionMethods
{
    public static IEnumerable<Control> FindControls(this Control root)
    {
        foreach (Control ctrl in root.Controls)
        {
            yield return ctrl;
            foreach (Control desc in ctrl.FindControls())
                yield return desc;
        }

    }
}

Now you can use it directly on any control and even apply Linq on the result since it's an IEnumerable.

This will get you an array of all controls in a page:

var allControls = this.FindControls().ToArray();

To get an array of all TextBox controls:

var allTextBoxes = this.FindControls()
                       .OfType<TextBox>().ToLArray<TextBox>();

and to get a list of TextBoxes with a specific id:

var myTextBox = this.FindControls()
                    .OfType<TextBox>()
                    .Where<TextBox>(tb => tb.ID.Equals("textBox1")).ToList<TextBox>();

You can also use it in foreach statements:

foreach (Control c in this.FindControls())
{
  ...
}
Sani Singh Huttunen
  • 21,744
  • 5
  • 66
  • 73
  • come on that i know i can access them by Id, See I am trying to get them using Page.Controls,on code behind of content page. But though they are showing me the controls of master page rather than content page.I want to acccess the Control Collection of content page who have type TextBox. please guide me – Red Swan Dec 22 '09 at 05:36
  • I asked just how to get the Textbox control collection of form which is given as Content page to a master page. no one can answer ? – Red Swan Jan 13 '10 at 07:33
  • Do you want to access the TextBox controls on the Content page from the Master page? – Sani Singh Huttunen Jan 13 '10 at 11:32