Comment puis-je utiliser la méthode FindControl dans asp.net?

Comment puis-je utiliser findControl et comment puis-je obtenir de l'id selon la méthode FindControl? j'ai besoin d'obtenir toutes les TextBox de données, il y a 40 zone de texte. Et TextBoxid de données...

je reall voulez apprendre aussi la méthode linq 😉

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

        }

le texte d'alt http://i49.tinypic.com/2mfb4ew.png

j'ai besoin de seulement ceci :

    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);
            }

TOUTE ma 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);
}
}
}
}
}
  • Si tout ce que vous essayez de faire ici est d'afficher les propriétés d'un objet donné par une réflexion sur votre page, et de les éditer, pourquoi ne pas utiliser le contrôle FormView et databind à vos données?
InformationsquelleAutor Penguen | 2010-07-01