Penguen
Penguen

Reputation: 17288

How can i use FindControl method in asp.net?

How can i use findControl and how can i get id's according to FindControl method? i need to get all TextBox data there are 40 textbox. And TextBoxid data...

i reall want to learn also linq method ;)

  protected void Button1_Click(object sender, EventArgs e)
        {
          //  SetRecursiveTextBoxAndLabels(PlaceHolder1);
            CreateForm creater = new CreateForm();
            creater.Holder = PlaceHolder1;
            creater.SetAccessForm();

            if (PlaceHolder1.Controls.Count > 0)
            {
                foreach (Control item in PlaceHolder1.Controls)
                {
                    item.FindControl(item.ID) is TextBox-------> How can i control is textBox?  Because there are labels grid.... 
                }
            }

        }

alt text

i need only this :

    ENG_ACCESS engAccess = new ENG_ACCESS();
            Type engTyp = engAccess.GetType();
            PropertyInfo[] properties = engTyp.GetProperties();

            TextBox txtbox = new TextBox();

            foreach (PropertyInfo strColumn in properties)
              {
                  txtbox = (TextBox)Page.FindControl("txt" + strColumn.Name);
                ListBox1.Items.Add(txtbox.Text);
            }

My WHOLE CODES:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

namespace RecursiveAddTextBox
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var dataCtx = new DataClasses1DataContext())
            {

                if (!IsPostBack)
                {
                    SetRecursiveTextBoxAndLabels();
                }



            }

        }

        void SetRecursiveTextBoxAndLabels()
        {
            TextBox txtBox;
            Label lbl;

                ENG_ACCESS eng = new ENG_ACCESS();
                Type typ = eng.GetType();
                PropertyInfo[] properties = typ.GetProperties();
                PlaceHolder1.Controls.Add(new LiteralControl("<table>"));
                for( int i =0;  i<properties.Length; i++)
                {
                    lbl = new Label();
                    lbl.ID = "lbl" + properties[i].Name;
                    lbl.Text = properties[i].Name;
                    PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
                    PlaceHolder1.Controls.Add(lbl);
                    PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
                    txtBox = new TextBox();
                    txtBox.ID ="txt"+properties[i].Name;
                    PlaceHolder1.Controls.Add(txtBox);
                    PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
                }
                PlaceHolder1.Controls.Add(new LiteralControl("</table>"));

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           SetRecursiveTextBoxAndLabels();



        }
        protected void Button2_Click(object sender, EventArgs e)
        {

            if (PlaceHolder1.Controls.Count > 0)
            {
                foreach (Control item in PlaceHolder1.Controls)
                {
                    if (item is TextBox)
                    {
                        TextBox t1 = (TextBox)PlaceHolder1.FindControl(item.ID);
                    }
                }
            }

        }

    }

Upvotes: 0

Views: 42129

Answers (4)

Mehdei
Mehdei

Reputation: 245

protected void Button1_Click(object sender, EventArgs e) 
        { 
          //  SetRecursiveTextBoxAndLabels(PlaceHolder1); 
            CreateForm creater = new CreateForm(); 
            creater.Holder = PlaceHolder1; 
            creater.SetAccessForm(); 

            if (PlaceHolder1.Controls.Count > 0) 
            { 
                foreach (Control item in PlaceHolder1.Controls) 
                { 
                     if (item is TextBox)
                         TextBox t1=(TextBox)PlaceHolder1.FindControl(item.ID);
                } 
            } 
        }

Upvotes: 1

Dave Thieben
Dave Thieben

Reputation: 5427

Add this extension method:

    /// <summary>
    /// Finds all controls with the given type anywhere under the root
    /// </summary>
    public static IList<Control> FindControlsRecursive<FindType>( this ControlCollection root )
    {
        Type toFind = typeof( FindType );
        List<Control> controls = new List<Control>();
        if ( root != null && root.Count > 0 )
        {
            foreach ( Control control in root )
            {
                if ( control != null && ( toFind.IsAssignableFrom( control.GetType() ) ) )
                {
                    controls.Add( control );
                }
                if ( control != null )
                {
                    controls.AddRange( control.Controls.FindControlsRecursive<FindType>() );
                }
            }
        }
        return controls;
    }

then call it with:

var textBoxes = Page.Controls.FindControlsRecursive<TextBox>();
foreach(var tb in textBoxes)
{
    tb.Text = "";
}

Upvotes: 0

matt-dot-net
matt-dot-net

Reputation: 4244

Why are you looping through each control and then asking the page to find the control you already have?

In other words, if (item is TextBox) then item is the TextBox you are looking for!

Remember that FindControl is not recursive so you have to call FindControl on the instance of the naming container that actually contains the control you want to find.

Upvotes: 0

Ben Cawley
Ben Cawley

Reputation: 1636

As an extension to the answer provided by Pranay, I think you are probably falling foul of INamingContainer.

The asp.net control ID's are created using nested controlnames. Take a look at MSDN for more information on webcontrols and the INamingContainer interface

Upvotes: 0

Related Questions