We help IT Professionals succeed at work.

How can i use FindControl method in asp.net?

programmerist
programmerist used Ask the Experts™
on
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 ;)
Loook please : http://i49.tinypic.com/2mfb4ew.png
  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....
                }
            }

        }

i ONLY NEED 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);
            }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
You can figure out if a control is a specific type with the following code
if(item.FindControl(item.ID).getType() == typeof(TextBox))
{
//do something
}

Open in new window


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

            TextBox txtbox = null;

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

Open in new window

If all of your controls are sitting in the same container a simple LINQ query will do what you want. You don't need the FindControl method as you suggested....



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DemoWebApp.HomePage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="formHomePage" runat="server">
    <div>
		<table>
			<tr>
				<td><asp:Label runat="server" ID="lblENG_ACCESS_WHATEVER1" Text="Some Label"></asp:Label></td>
				<td><asp:TextBox runat="server" ID="txtENG_ACCESS_WHATEVER1" Text="Some TextBox 1"></asp:TextBox></td>
			</tr>
			<tr>
				<td><asp:Label runat="server" ID="lblENG_ACCESS_WHATEVER2" Text="Some Label"></asp:Label></td>
				<td><asp:TextBox runat="server" ID="txtENG_ACCESS_WHATEVER2" Text="Some TextBox 2"></asp:TextBox></td>
			</tr>
			<tr>
				<td><asp:Label runat="server" ID="lblENG_ACCESS_WHATEVER3" Text="Some Label"></asp:Label></td>
				<td><asp:TextBox runat="server" ID="txtENG_ACCESS_WHATEVER3" Text="Some TextBox 3"></asp:TextBox></td>
			</tr>
			<tr>
				<td><asp:Label runat="server" ID="lblENG_ACCESS_WHATEVER4" Text="Some Label"></asp:Label></td>
				<td><asp:TextBox runat="server" ID="txtENG_ACCESS_WHATEVER4" Text="Some TextBox 4"></asp:TextBox></td>
			</tr>
			<tr>
				<td><asp:Label runat="server" ID="lblENG_ACCESS_WHATEVER5" Text="Some Label"></asp:Label></td>
				<td><asp:TextBox runat="server" ID="txtENG_ACCESS_WHATEVER5" Text="Some TextBox 5"></asp:TextBox></td>
			</tr>
		</table>
    </div>
    <asp:ListBox ID="ListBoxTextBoxContents" runat="server" Height="141px" Width="213px"></asp:ListBox>
    </form>
</body>
</html>





using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DemoWebApp
{
	public partial class HomePage : Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			foreach (var control in 
				formHomePage.Controls.Cast<Control>()
				.Where(¿ => ¿.ID != null && ¿.ID.StartsWith("txtENG_ACCESS"))
				.Cast<TextBox>())
			{
				ListBoxTextBoxContents.Items.Add(control.Text);
			}

			// - OR - 
			ListBoxTextBoxContents.Items.AddRange((from control in formHomePage.Controls.Cast<Control>()
			        where control.ID != null && control.ID.StartsWith("txtENG_ACCESS")
			        let textBox = control as TextBox
			        select new ListItem(textBox.Text)).ToArray());

			// - OR - 

			ListBoxTextBoxContents
				.Items
				.AddRange(
					formHomePage.Controls.Cast<Control>()
					.Where(¿ => ¿.ID != null && ¿.ID.StartsWith("txtENG_ACCESS"))
					.Cast<TextBox>()
					.Select(¿ => new ListItem(¿.Text))
					.ToArray());
		}
	}
}

Open in new window

Sorry, I use the lambda character when doing LINQ statements a lot. That upside-down question mark should be replaced like so...



using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DemoWebApp
{
	public partial class HomePage : Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			foreach (var control in 
				formHomePage.Controls.Cast<Control>()
				.Where(c => c.ID != null && c.ID.StartsWith("txtENG_ACCESS"))
				.Cast<TextBox>())
			{
				ListBoxTextBoxContents.Items.Add(control.Text);
			}

			// - OR - 
			ListBoxTextBoxContents.Items.AddRange((from control in formHomePage.Controls.Cast<Control>()
			        where control.ID != null && control.ID.StartsWith("txtENG_ACCESS")
			        let textBox = control as TextBox
			        select new ListItem(textBox.Text)).ToArray());

			// - OR - 

			ListBoxTextBoxContents
				.Items
				.AddRange(
					formHomePage.Controls.Cast<Control>()
					.Where(c => c.ID != null && c.ID.StartsWith("txtENG_ACCESS"))
					.Cast<TextBox>()
					.Select(tb => new ListItem(tb.Text))
					.ToArray());
		}
	}
}

Open in new window

If your TextBox controls are in various containers on your page let me know and I'll show you how to do that with a single LINQ query as well.
I'm sorry, I just realized I missed the reflection part of your question. Just put that in your query also like so...



	public class ENG_ACCESS
	{
		public string ENG_ACCESS_WHATEVER1 { get; set; }
		public string ENG_ACCESS_WHATEVER2 { get; set; }
		public string ENG_ACCESS_WHATEVER3 { get; set; }
		public string ENG_ACCESS_WHATEVER4 { get; set; }
		public string ENG_ACCESS_WHATEVER5 { get; set; }
	}

	public partial class HomePage : Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			ListBoxTextBoxContents.Items.AddRange((from property in typeof(ENG_ACCESS).GetProperties()
												   from control in formHomePage.Controls.Cast<Control>()
												   where control.ID != null && control.ID.StartsWith("txt" + property.Name)
												   let textBox = control as TextBox
												   select new ListItem(textBox.Text)).ToArray());

.
.
.

Open in new window